I am working on re-writing an existing web site using Node.js with Express.
Front-end of the site will use Backbone JS and thus I need to have all necessary routes comply with native Backbone sync. Now most of URL’s the client and for Backbone sync will be same. But they won’t work for regular GET as they would need to return JSON.
So I am thinking, would it be a good idea to add extension to Model/Collection URLs in Backbone, such as .json, and in Express to have this for every route:
app.get('/p/:topCategory/:category/:product.:format', function(req, res) { ... });
Where if (req.params.id == 'json') than we send JSON, otherwise we render HTML?
Or is there a better approach? Please help.
The better way of doing this would be to use the content negotiation feature in Express 3.x, namely
res.format:https://github.com/visionmedia/express/blob/master/lib/response.js#L299-378
You approach is also ok, Yammer for ex. is using the same approach: http://developer.yammer.com/api/#message-viewing