简体   繁体   English

为请求后端的自定义标头配置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 https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md

Angular-cli version: Angular-cli版本:

....
  "devDependencies": {
    "@angular/cli": "1.2.0",
....

Created the file: proxy.conf.json 创建文件:proxy.conf.json

{
  "/profile/*": {
    "target": "http://localhost:8888",
    "secure": false,
    "pathRewrite": {
      "^/profile": ""
    },
    "changeOrigin": true,
    "logLevel": "debug"
  }
}

I registered it in package.json 我在package.json中注册了它

....
  "scripts": {
    "ng": "ng",
    "start": "ng serve --proxy-config proxy.conf.json",
....

Starting application as follows: npm start 启动应用程序如下: npm start

Here ist the start log: 在这里启动日志:

> 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.

And the problem is definitely not in the backend, because the cors is configured there. 而且问题绝对不在后端,因为在此处配置了cors。 I monitor my request with Fiddler. 我通过Fiddler监视我的请求。

Here's how it looks now: 现在是这样的:

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

We are talking about this header: x-auth-token 我们正在谈论此标头: x-auth-token

I also read this topic. 我也阅读了这个话题。

angular-cli server - how to proxy API requests to another server? angular-cli服务器-如何将API请求代理到另一台服务器?

Any Ideas? 有任何想法吗? Thanks. 谢谢。

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. 我不得不根据上述答案进行小调整,尽管现在看配置有些奇怪。

This is my proxy.conf.json shown below: 这是我的proxy.conf.json,如下所示:

{
  "/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. 现在就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM