Home > Software design >  'Access-Control-Allow-Origin' in pure nodejs
'Access-Control-Allow-Origin' in pure nodejs

Time:09-20

I want to know how I can implement CORS policy for my pure NodeJS app, no Expressjs. A simple implementation code snippet or idea would be very helpful.

CodePudding user response:

https.createServer(options, (req, res) => {
  res.writeHead(200, {
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
  });
  res.end(JSON.stringify({
     version: '1.0',
     endpoint: req.url,
     method: req.method,
     headers: req.headers 
  }, null, 2));
}).listen(PORT);
  • Related