Skip to content

Instantly share code, notes, and snippets.

@cuppster
Created April 9, 2012 16:02
Show Gist options
  • Save cuppster/2344435 to your computer and use it in GitHub Desktop.
Save cuppster/2344435 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);
@hygull
Copy link

hygull commented Sep 9, 2018

Great, it is helpful.

@isaquebc
Copy link

isaquebc commented Nov 20, 2018

Very good!

@Aubizzy
Copy link

Aubizzy commented Jun 17, 2020

This is Great stuff. it worked for me

@HarryLit
Copy link

Great, it works! Thank you.

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