Skip to content

Instantly share code, notes, and snippets.

@arturparkhisenko
Forked from balupton/cors.js
Created February 27, 2019 16:19
Show Gist options
  • Save arturparkhisenko/e3d1023b793ca863af7a35a724af3a36 to your computer and use it in GitHub Desktop.
Save arturparkhisenko/e3d1023b793ca863af7a35a724af3a36 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