Skip to content

Instantly share code, notes, and snippets.

@bmdayal
Created November 9, 2015 23:02
Show Gist options
  • Save bmdayal/93cad3256ca0377f83b0 to your computer and use it in GitHub Desktop.
Save bmdayal/93cad3256ca0377f83b0 to your computer and use it in GitHub Desktop.
//CORS on ExpressJS: http://enable-cors.org/server_expressjs.html
//Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://addressbookclient.azurewebsites.net');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment