问题
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