Skip to content

Instantly share code, notes, and snippets.

@muhammadghazali
Created April 22, 2013 16:18
Show Gist options
  • Save muhammadghazali/5436435 to your computer and use it in GitHub Desktop.
Save muhammadghazali/5436435 to your computer and use it in GitHub Desktop.
An Express middleware to check the requested content type.
/**
* A middleware to check the requested content type.
* Note:
* Just be sure to place the middleware before:
* app.use(app.router);
*/
module.exports.acceptedContentType = function () {
return function (req, res, next) {
res.format({
'application/xml': function () {
next();
},
'application/json': function () {
next();
},
'default': function () {
res.send(406, 'Sorry, we only provide resources in JSON and XML.');
}
});
};
};
@muhammadghazali
Copy link
Author

You might want to read a great article about how to write middleware for Express:
http://www.hacksparrow.com/how-to-write-midddleware-for-connect-express-js.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment