Does anybody know a way in express.js to capture requests in a single function for both html and json?
Essentially I want a single route for both /users and /users.json – like rails does with its routes -> controller.
That way, I can encapsulate the logic in a single function and decide to render either html or json.
Something like:
app.get('/users[.json]', function(req, res, next, json){
if (json)
res.send(JSON.stringfy(...));
else
res.render(...); //jade template
});
Could I use a param perhaps?
A route is simple a string which is compiled to a RegExp internally, as the manual says, so you can do something like this:
Check more here: http://expressjs.com/guide.html#routing