Skip to content

Instantly share code, notes, and snippets.

@m-philipp
Created September 11, 2014 18:49
Show Gist options
  • Select an option

  • Save m-philipp/07b396ab13b0d8535dc5 to your computer and use it in GitHub Desktop.

Select an option

Save m-philipp/07b396ab13b0d8535dc5 to your computer and use it in GitHub Desktop.
Adding CORS to express
function allowCrossDomain(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'x-requested-with, Content-Type, Auth-Token');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.send(204);
}
else {
if (req.header('x-requested-with')) {
res.header('x-requested-with', req.header('x-requested-with'));
}
next();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment