I'm having difficulty using PushState with backbone and restify. Most of my routes work fine, but there is a conflict on the GET route for a single model. I've worked around this by having restify only serve those that have an XMLHttpRequest header set.
server.js
var app = restify.createServer();
app.use(restify.bodyParser());
// serve static files
app.get(/^\/public\/(.*)/, public.serveFile);
// Read and handle 'post' model
app.get('/posts/:id', function (req, resp, next) {
// process backbone requests
if (req.headers['x-requested-with'] === 'XMLHttpRequest') {
posts.single(req, resp, next);
} else {
index(req, resp, next);
}
});
// a catch all that gives backbone control
app.get(/.*/, index);
Backbone Router
// pushstate is true
routes: {
'': 'home',
'posts/:id': 'show', // issues
'*other': 'default'
},
I could have backbone handle all requests on /posts/ and then create a seperate api route to handle the basic CRUD operations, but i'd like to preserve a parity if possible.
My current solution works, but it seems wonky, there a better way to handle this?