node 跨域请求设置

时间:2022-11-14 17:24:38
http.createServer((req,res)=>{
//设置允许跨域的域名,*代表允许任意域名跨域
res.setHeader("Access-Control-Allow-Origin","*");
//跨域允许的header类型
res.setHeader("Access-Control-Allow-Headers","Content-type,Content-Length,Authorization,Accept,X-Requested-Width");
//跨域允许的请求方式
res.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
//设置响应头信息
res.setHeader("X-Powered-By",' 3.2.1')
//让options请求快速返回
if(req.method == "OPTIONS"){return res.end();}
}
}).listen(3000);