node.js 反应代理错误:无法将请求 /api/ 从 localhost:3000 代理到 http://localhost:8000 (ECONNREFUSED)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50107816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React Proxy error: Could not proxy request /api/ from localhost:3000 to http://localhost:8000 (ECONNREFUSED)
提问by cclloyd
I have a React frontend that uses jwt to authenticate with the Django backend. The backend works and is connecting just fine using django views, but when I try to proxy a request from React, it gives me a Connection Refused error.
我有一个 React 前端,它使用 jwt 与 Django 后端进行身份验证。后端工作并且使用 django 视图连接得很好,但是当我尝试代理来自 React 的请求时,它给了我一个连接拒绝错误。
Proxy error: Could not proxy request /api/auth/token/obtain/ from localhost:3000 to http://localhost:8000(ECONNREFUSED).
代理错误:无法将请求 /api/auth/token/obtain/ 从 localhost:3000 代理到http://localhost:8000(ECONNREFUSED)。
Connecting to http://localhost:8000/api/auth/token/obtain/works normally. And sending a POST request with Axios also works normally and returns the token json. But when I proxy it with node, it doesn't work.
连接到http://localhost:8000/api/auth/token/obtain/工作正常。并且用axios发送POST请求也正常,返回token json。但是当我用节点代理它时,它不起作用。
In my package.jsonI have:
在我的package.json我有:
"proxy": {
"/api/*": {
"target": "http://localhost:8000"
}
},
Edit: Public repo. You can run easily if you have docker installed. (uses 1 image and 2 containers). After cloning just run docker-compose build, then docker-compose up.
编辑: 公共回购。如果安装了 docker,则可以轻松运行。(使用 1 个图像和 2 个容器)。克隆后只要运行docker-compose build,然后docker-compose up。
Edit2: Headers of request:
编辑2:请求标题:
*General*
Request URL: http://localhost:3000/api/auth/token/obtain/
Request Method: POST
Status Code: 500 Internal Server Error
Remote Address: [::1]:3000
Referrer Policy: no-referrer-when-downgrade
*Response Headers*
HTTP/1.1 500 Internal Server Error
X-Powered-By: Express
Date: Mon, 30 Apr 2018 21:23:17 GMT
Connection: keep-alive
Transfer-Encoding: chunked
*Request Headers
POST /api/auth/token/obtain/ HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 45
Pragma: no-cache
Cache-Control: no-cache
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
Content-Type: application/json
Accept: */*
Referer: http://localhost:3000/login
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,fr;q=0.8,ja;q=0.7
采纳答案by cclloyd
So the issue was since both the Node dev environment and the Django dev environment were running in separate docker containers, so localhostwas referring to the node container, not the bridged network.
所以问题是因为 Node 开发环境和 Django 开发环境都在单独的 docker 容器中运行,所以localhost指的是节点容器,而不是桥接网络。
So the key was to use container links, which are automatically created when using docker-compose, and use that as the hostname. So I changed it to
所以关键是使用容器链接,在使用时自动创建docker-compose,并将其用作主机名。所以我把它改成
"proxy": {
"/api": {
"target": "http://django:8000"
}
},
And that worked, as long as you launch both containers with the same docker-composecommand, otherwise you have to manually specify external_links in your docker-compose.ymlfile.
只要您使用相同的docker-compose命令启动两个容器,就可以奏效,否则您必须在docker-compose.yml文件中手动指定 external_links 。
回答by VincentJr
I'm running into the same problem as well. Most search results mention adding "secure": falseor "ignorePath": trueto your proxy config. Something like this:
我也遇到了同样的问题。大多数搜索结果都提到添加"secure": false或添加"ignorePath": true到您的代理配置。像这样的东西:
"proxy": {
"/api/*": {
"target": "http://localhost:8000",
"secure": false
}
},
May be worth a try but unfortunately none of this worked for me. Although each address (http://localhost:3000and http://localhost:8000) work completely fine in the browser, maybe since the container is actually proxying it needs to use a Docker address?
可能值得一试,但不幸的是,这些都不适合我。尽管每个地址(http://localhost:3000和http://localhost:8000)在浏览器中都可以正常工作,但也许由于容器实际上是代理它需要使用 Docker 地址?
EDIT--
编辑 -
Alright I think I figured it out. I believe it did have to do with the container to container communication. Looking in your docker-compose, your api server is called django. Change your package.json file to this:
好吧,我想我想通了。我相信它确实与容器到容器的通信有关。查看您的docker-compose,您的 api 服务器被称为django。将您的 package.json 文件更改为:
"proxy": {
"/api/*": {
"target": "http://django:8000",
"secure": false
}
}
回答by ralphinator80
If your on a newer version CRA 2.0+ you'll need to do this via a manual proxy. https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development#configuring-the-proxy-manually
如果您使用的是较新版本的 CRA 2.0+,则需要通过手动代理执行此操作。 https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development#configuring-the-proxy-manually

