美文网首页
前后台跨域通信配置(express +vue)

前后台跨域通信配置(express +vue)

作者: 小二儿上酒 | 来源:发表于2019-03-14 16:54 被阅读0次

express配置

// cors
app.use("*", 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)
  } else {
    next()
  }
});

vue配置

proxyTable: {
      '/api': {
        // 测试环境
        target: 'http://127.0.0.1:3000/',  // 接口域名
        changeOrigin: true,  //是否跨域
        pathRewrite: {
            '^/api': ''   //需要rewrite重写的,
        }
      }         
    },

相关文章

网友评论

      本文标题:前后台跨域通信配置(express +vue)

      本文链接:https://www.haomeiwen.com/subject/ghscmqtx.html