express 跨域的问题

最近在学习nodejs,用到express-generator 生成的一个项目。但是在前端发起请求的出现了跨域的问题。前端是8080端口,而后端是3000端口,必然会出现跨域的问题。

var allowCrossDomain = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', 'http://localhost:8080');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    res.header('Access-Control-Allow-Credentials','true');
    next();
};
app.use(allowCrossDomain);

参考资料

你可能感兴趣的:(express 跨域的问题)