Node处置惩罚http跨域要求

因为项目须要,我用Node写的背景须要处置惩罚http跨域要求,处理体式格局以下:

app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    if(req.method=="OPTIONS") res.send(200);/*让options要求疾速返回*/
    else  next();
});

实在道理就是在返转头里到场许可跨域接见的返转头参数,

1是Access-Control-Allow-Origin 许可的域
2是Access-Control-Allow-Headers 许可的header范例
3是Access-Control-Allow-Methods 许可的要求要领

这三项都可以设置为”*”示意接收恣意范例的要求

    原文作者:flyer_dev
    原文地址: https://segmentfault.com/a/1190000005071028
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞