Created
April 22, 2013 16:18
-
-
Save muhammadghazali/5436435 to your computer and use it in GitHub Desktop.
An Express middleware to check the requested content type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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.'); | |
} | |
}); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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