How set proxy headers in proxy.config.json file for angularcli project

放肆的年华 提交于 2020-05-14 17:49:49

问题


I'm trying to set proxy headers for angularcli. Here's what I have so far in my proxy.config.json file:

"/api": {
    "target": "https://applications.str.coni.com/api",
    "secure": false,
    "logLevel": "debug"

But I haven't had any luck so far, perhaps I'm missing something (probably in another file). Any suggestions would be much appreciated.


回答1:


Angular-cli uses http-proxy-middleware https://github.com/chimurai/http-proxy-middleware

there is an option called headers that you can use: https://github.com/chimurai/http-proxy-middleware#http-proxy-option

example:

"/api":
  {
    "target": "https://applications.str.coni.com/api",
    "secure": false,
    "logLevel": "debug",
    "headers": {"host":"www.example.org"}
  }



回答2:


You can modify both request and response headers and request is easy with the code from @AhmedMusallam answer but for response headers you'll need to change file to proxy.config.js and modify proxy config file to look like this:

const PROXY_CONFIG = {
  "/api": {
      "target": "https://applications.str.coni.com/api",
      "secure": false,
      "logLevel": "debug",
      "onProxyRes": function (proxyRes, req, res) {
        proxyRes.headers['Access-Control-Allow-Headers'] = 'Authorization';
      },
    },
};

module.exports = PROXY_CONFIG;


来源:https://stackoverflow.com/questions/43591539/how-set-proxy-headers-in-proxy-config-json-file-for-angularcli-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!