为后端请求中的自定义标头配置 Angular-cli 代理? [英] Configure Angular-cli proxy for custom headers in request to backend?
问题描述
这个话题,据我所知,它非常新鲜和相关.告诉我我的错误在哪里?
the topic, as I understand it is quite fresh and relevant. Tell me where is my mistake?
所以,我按照文档中的说明做了所有事情:
So, I did everything as in the documentation:
https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md
Angular-cli 版本:
Angular-cli version:
....
"devDependencies": {
"@angular/cli": "1.2.0",
....
创建文件:proxy.conf.json
Created the file: proxy.conf.json
{
"/profile/*": {
"target": "http://localhost:8888",
"secure": false,
"pathRewrite": {
"^/profile": ""
},
"changeOrigin": true,
"logLevel": "debug"
}
}
我在 package.json 中注册了它
I registered it in package.json
....
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxy.conf.json",
....
启动应用程序如下:npm start
这里是启动日志:
> ng serve --proxy-config proxy.conf.json
** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200 **
10% building modules 3/3 modules 0 active[HPM] Proxy created: /profile -> http://localhost:8888
[HPM] Proxy rewrite rule created: "^/profile" ~> ""
[HPM] Subscribed to http-proxy events: [ 'error', 'close' ]
Hash: 2f1f9b69df46574b900e
Time: 12544ms
chunk {0} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 160 kB {4} [initial] [rendered]
chunk {1} main.bundle.js, main.bundle.js.map (main) 131 kB {3} [initial] [rendered]
chunk {2} styles.bundle.js, styles.bundle.js.map (styles) 255 kB {4} [initial] [rendered]
chunk {3} vendor.bundle.js, vendor.bundle.js.map (vendor) 3.79 MB [initial] [rendered]
chunk {4} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered]
webpack: Compiled successfully.
而且问题肯定不在后端,因为cors是在那里配置的.我使用 Fiddler 监控我的请求.
And the problem is definitely not in the backend, because the cors is configured there. I monitor my request with Fiddler.
这是现在的样子:
OPTIONS http://localhost:8888/profile/data/personal HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
x-ijt: c2q0qqq02it9p2jrk3m6ihbs5u
Access-Control-Request-Headers: content-type,x-auth-token
Accept: */*
Referer: http://localhost:4200/
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
我们正在谈论这个标题:x-auth-token
We are talking about this header: x-auth-token
我也读过这个话题.
angular-cli 服务器 - 如何代理 API 请求到另一台服务器?
有什么想法吗?谢谢.
推荐答案
重新发布我在其他问题中发布的答案.
Reposting the answer I posted in that other question.
我不得不根据上述答案进行一些小的调整,尽管现在查看配置似乎有点奇怪.
I had to make a small adjustment based on the above answers, although it seems a bit odd looking at the config now.
这是我的proxy.conf.json,如下所示:
This is my proxy.conf.json shown below:
{
"/api/*": {
"target": "https://url.com",
"secure": false,
"changeOrigin": true,
"logLevel": "debug",
"pathRewrite": {"^/api" : "http://url.com/api"}
}
}
基本上,我完全重写了路径.现在可以使用了.
Basically, I rewrote the path completely. And it works now.
这篇关于为后端请求中的自定义标头配置 Angular-cli 代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!