25

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?

Chenmunka
  • 685
  • 4
  • 21
  • 25
justinjmoses
  • 487
  • 1
  • 5
  • 11

3 Answers3

55

I believe res.format() is the way to do this in Express 3.x and 4.x:

res.format({
  text: function(){
    res.send('hey');
  },

  html: function(){
    res.send('<strong>hey</strong>');
  },

  json: function(){
    res.send({ message: 'hey' });
  }
});

This relies on the Accept header, however you can munge this header yourself using a custom middleware or something like connect-acceptoverride.

One example of a custom middleware might be:

app.use(function (req, res, next) {
  var format = req.param('format');

  if (format) {
    req.headers.accept = 'application/' + format;
  }

  next();
});
Beau
  • 11,267
  • 8
  • 44
  • 37
20

A route is simple a string which is compiled to a RegExp internally, as the manual says, so you can do something like this:

app.get("/users/:format?", function(req, res, next){
  if (req.params.format) { res.json(...); }
  else {
    res.render(...); //jade template
  }
});

Check more here: http://expressjs.com/guide.html#routing

alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • 5
    ah right - found the answer in the express guide. app.get('/users.:format?', function(req, res){ if (req.params.format == 'json') res.send(JSON.stringify(...)); else res.render(...); //jade template }); – justinjmoses Jan 19 '12 at 15:50
  • i just found it at the same time - but appreciate the response. – justinjmoses Jan 19 '12 at 15:50
  • 1
    in case anyone runs into this issue like I did, make sure your route is defined as /users.:format? and not /users:format? if you want to differentiate users vs users.json. otw you will have to specify users/json. – emilebaizel Nov 18 '13 at 23:06
  • @justinjmoses Note that it's `JSON.stringify`, not `JSON.stringfy`. – Nick McCurdy Apr 24 '14 at 04:19
  • if I have route defined as '/users/:userId.:format' it doesn't catch request without specifying extension e.g '/users/123' . Is it possible to catch this request by the same route? – matus Nov 13 '14 at 20:43
  • Normally if you want some placeholder to be optional just add a question mark "?", but in your example there's also a dot '.' in there. You can always default to a regular expression instead of a regular "placeholdery" Express route. – alessioalex Nov 13 '14 at 22:29
1

I was not happy with the above answers. Here's what I did instead. Please vote up if it helps you.

I just make sure that all json requests have the Content-Type header set to 'application/json'.

if (req.header('Content-Type') == 'application/json') {
  return res.json({ users: users });
} else {
  return res.render('users-index', { users: users });
}
ajbraus
  • 2,909
  • 3
  • 31
  • 45