首页 » 小蓝 » Express全局跨域代码段

Express全局跨域代码段

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");
    res.header("X-Powered-By",' 3.2.1');
    if(req.method=="OPTIONS") res.send(200);
    else  next();
});

通过上面代码设置全部路由都允许跨域,每个参数的含义,自行搜索。
马克专用