Skip to content

Instantly share code, notes, and snippets.

@ehansen31
Last active August 9, 2017 12:26
Show Gist options
  • Save ehansen31/20ac5663551678f12b8afd7dc22fc6bd to your computer and use it in GitHub Desktop.
Save ehansen31/20ac5663551678f12b8afd7dc22fc6bd to your computer and use it in GitHub Desktop.
Snippet that should be placed after app.use(express) that should allow cors from any domain
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment