2808 explained code solutions for 75 technologies


nodejsHow to allow CORS in HTTP server


const http = require('http');

const server = http.createServer((req, res) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
  res.setHeader('Access-Control-Max-Age', 60*60*24*30);
  
  res.end('Hi');
});

server.listen(82);ctrl + c
require('http')

import module to work with http protocol

http.createServer

creates HTTP server

(req,

object with request data

res

object to manage response

setHeader(

sets response header

Access-Control-

CORS headers to set

'*'

allow access from all origins

60*60*24*30

ACL lifetime is set to 30 days