Configure REST routes in Express JS for Ajax for use only with Backbone

I am working on re-recording an existing website using Node.js using Express.

The front of the site will use Backbone JS, and so I need all the necessary routes to match the built-in synchronous synchronization. Now, most of the client URL and for synchronization with the database will be the same. But they will not work for a regular GET, since they will need to return JSON.

So, I think it would be nice to add the extension to the Model / Collection URLs in Backbone, e.g. .json and Express, to have this for each route:

 app.get('/p/:topCategory/:category/:product.:format', function(req, res) { ... }); 

Where if (req.params.id == 'json') than we send JSON, otherwise we will display HTML?

Or is there a better approach? Please, help.

+7
source share
4 answers

The best way to do this is 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

 res.format({ text: function(){ res.send('hey'); }, html: function(){ res.send('<p>hey</p>'); }, json: function(){ res.send({ message: 'hey' }); } }); 

You come too, Yammer for ex. uses the same approach: http://developer.yammer.com/api/#message-viewing

+12
source

Use the Accept headers in your requests: Accept: application/json if you want to receive JSON, Accept: text/HTML if you want HTML.

+6
source

An alternative that also verifies that the "X-Requested-With" header is set for jQuery et al.

 var onlyAllowJsonRequests = function (req, res, next) { var acceptJson = (req.accepted.length && _.any(req.accepted, function (acc) { return acc.value.indexOf("json") !== -1 })); // also check that "X-Requested-With": "XMLHttpRequest" header is set if (acceptJson && (req.xhr === true)) { next(); } else { res.send(406, "Not Acceptable"); } }; app.use(onlyAllowJsonRequests); 

Underline NB is a parameter.

+2
source

I think the right way to do this is to implement content alignment in your application. Yes, the Express 3.x tool is a way to do this and gives a direct answer to your question, but I don’t think that this is the best way to do this because it transfers responsibility for content matching into the routing logic. I do not think this is a good place, because it does not comply with the principle of sole responsibility , putting content harmonization in the routing logic.

I took a hit on implementing content alignment on my blog; which can help you in the right direction. The bottom line is that the code determines the file extension using content negotiation . Then, with the file extension, he wants to find the corresponding view file, displays it in response and sends it back to the client. The idea is that it responds with the requested resource in the requested view for content matching. routing logic indicates only a view, but does not have a clue about content negotiation. This happens outside the routing logic, which provides a more flexible design.

The result of this project is the ability to request a specific representation of the resource, for example:

http://blog.joeyguerra.com/index.json and get a JSON representation of http://blog.joeyguerra.com/index.phtml and get a partial (or HTML snippet) HTML representation of http://blog.joeyguerra.com/ index.xml and get an XML view.

+2
source

All Articles