3

我的应用程序使用 Python 后端,因此当我在本地运行时,我需要将我的请求代理到与运行 Webpack 不同的端口。

我尝试了几种不同的方法来完成这项工作:

devServer: {
  contentBase: outDir,
  proxy: [
    {
      path: "**",
      target: "http://localhost:8000",
      secure: false,
      bypass: function(req, res, options) {
        if(req.url === '' || req.url === '/') {
          res.statusCode = 302;
          res.setHeader('Location', '/a/');
          return '/a/';
        }

        var frontend = new RegExp("^\/$^|\/a\/index\.html|^\/a\/|^\/a$|^\/styleguide");
        if (frontend.test(req.url)) return req.url;
      }
    }
  ],
  // serve index.html for all 404 (required for push-state)
  historyApiFallback: true,
},

和这个:

proxy: [{
  context: '/',
  target: 'http://localhost:8000',
}],

这两者至少会在启动时显示类似于此的消息:[HPM] Proxy created: ** -> http://localhost:8000. 当我查看文档并执行类似操作时,它也不起作用(我什至没有收到有关代理正在运行的消息):

proxy: {
  "/": "http://localhost:8000"
}

有什么明显的我做错了吗?我一直在尝试所有我能想到的组合来让它发挥作用。

4

1 回答 1

0

尝试添加changeOrigin: true. 不要忘记更改 API 模式。

proxy: {
  '/api/**': {
    target: 'http://localhost:8000',
    secure: false,
    changeOrigin: true
  },
}
于 2017-07-18T13:26:55.353 回答