Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save farvisun/75fd1db16c70cee1ba12ee66cce63e83 to your computer and use it in GitHub Desktop.
Save farvisun/75fd1db16c70cee1ba12ee66cce63e83 to your computer and use it in GitHub Desktop.
express.js middleware to support CORS pre-flight requests
app.use(express.methodOverride());
// ## CORS middleware
//
// see: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};
app.use(allowCrossDomain);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment