Skip to content

Instantly share code, notes, and snippets.

@dashawk
Forked from balupton/cors.js
Created June 19, 2020 04:15
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 dashawk/3bdf582659cffb9257ed5c0a22c6c588 to your computer and use it in GitHub Desktop.
Save dashawk/3bdf582659cffb9257ed5c0a22c6c588 to your computer and use it in GitHub Desktop.
Acheiving CORS via a Node HTTP Server
// Create our server
var server;
server = http.createServer(function(req,res){
// Set CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
res.setHeader('Access-Control-Allow-Headers', '*');
if ( req.method === 'OPTIONS' ) {
res.writeHead(200);
res.end();
return;
}
// ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment