node 跨域请求设置

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);

 

 
posted @ 2019-03-16 22:54  紫诺花开  阅读(3391)  评论(0编辑  收藏  举报