Skip to content

Instantly share code, notes, and snippets.

@ralucas
Last active August 29, 2015 14:05
Show Gist options
  • Save ralucas/7cbb71705f5a4a235e6c to your computer and use it in GitHub Desktop.
Save ralucas/7cbb71705f5a4a235e6c to your computer and use it in GitHub Desktop.
Allowing access control in Node.js
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};
app.use(allowCrossDomain);
// Access Control
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment