在node开发中,如果不用express脚手架搭建服务器,用node原生的语法,则有下面的处理跨域的方式
app.all('*', (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'x-Request-With,Content-Type')
res.header('Access-Control-Methods', 'PUT, POST, GET, DELETE, OPTIONS')
})
或者是在前端通过代理服务器跨域,下面这个配置是在webpack中配置的
devServer: {
proxy: 'http://localhost:4000'
}