Skip to content

Instantly share code, notes, and snippets.

Created November 14, 2014 05:41
Show Gist options
  • Save anonymous/59b9d5c3e41658c63f70 to your computer and use it in GitHub Desktop.
Save anonymous/59b9d5c3e41658c63f70 to your computer and use it in GitHub Desktop.
middleware example
var o2x = require('object-to-xml');
var j2c = require('json-2-csv');
function(req, res, next) {
if(req.accepts('application/json')) {
return res.json(req.payload);
}
if(req.accepts('text/xml')) {
res.set('content-type', 'text/xml');
return res.send(o2x(req.payload));
}
if(req.accepts('text/csv')) {
req.set('content-type', 'text/csv');
return res.send(j2c(req.payload));
}
return res
.status(406)
.send('Not Acceptable');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment