Angular2/Angular seed http-proxy-middleware proxy api requests

青春壹個敷衍的年華 提交于 2019-12-02 09:44:07

Spent a bit of time trying to get this work and this is what I ended up adding to project.config.ts within the constructor.

   /* Add proxy middleware */
    this.PROXY_MIDDLEWARE = [
      require('http-proxy-middleware')(
        '/basepath/api', {
        ws: false,
        onProxyReq: this.onProxyReq,
        target: 'http://localhost:7000',
        pathRewrite: {
          '^/basepath/api' : '/api'
        }
      })
    ];

And this is the function I included below the constructor to add header to any requests that are proxied

onProxyReq(proxyReq: any , req: any, res: any) {
  // add custom headers to request
  proxyReq.setHeader('header_name', 'value');
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!