Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Driptap
Forked from nilcolor/Node.js CORS
Created June 13, 2018 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Driptap/9247e2de266a70d8d6fd7b5cdf3fdf9d to your computer and use it in GitHub Desktop.
Save Driptap/9247e2de266a70d8d6fd7b5cdf3fdf9d to your computer and use it in GitHub Desktop.
Node.js cross-origin POST. You should response for OPTIONS request first. Something like this.
if (req.method === 'OPTIONS') {
console.log('!OPTIONS');
var headers = {};
// IE8 does not allow domains to be specified, just the *
// headers["Access-Control-Allow-Origin"] = req.headers.origin;
headers["Access-Control-Allow-Origin"] = "*";
headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
headers["Access-Control-Allow-Credentials"] = false;
headers["Access-Control-Max-Age"] = '86400'; // 24 hours
headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept";
res.writeHead(200, headers);
res.end();
} else {
//...other requests
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment