反应代理错误:无法将请求/api/从localhost:3000代理到http://localhost:8000(ECONNREFUSED) [英] React Proxy error: Could not proxy request /api/ from localhost:3000 to http://localhost:8000 (ECONNREFUSED)

查看:1037
本文介绍了反应代理错误:无法将请求/api/从localhost:3000代理到http://localhost:8000(ECONNREFUSED)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个React前端,它使用jwt与Django后端进行身份验证.后端可以正常工作,并且使用django视图可以很好地进行连接,但是当我尝试代理来自React的请求时,它给了我一个Connection Refused错误.

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.

代理错误:无法将请求/api/auth/token/obtain/从localhost:3000代理到 http://localhost:8000 (ECONNREFUSED).

Proxy error: Could not proxy request /api/auth/token/obtain/ from localhost:3000 to http://localhost:8000 (ECONNREFUSED).

连接到 http://localhost:8000/api/auth/token/obtain/正常工作.使用Axios发送POST请求也可以正常工作,并返回令牌json.但是当我用节点代理它时,它不起作用.

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.

我的package.json中有:

  "proxy": {
    "/api/*":  {
      "target": "http://localhost:8000"
    }
  },

公共存储库.如果您已安装docker,则可以轻松运行. (使用1张图片和2个容器).克隆后,只需运行docker-compose build,然后运行docker-compose up.

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.

Edit2:请求标头:

Headers of request:

*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

推荐答案

所以问题出在,因为Node dev环境和Django dev环境都在单独的docker容器中运行,所以localhost是指节点容器,而不是桥接网络.

So the issue was since both the Node dev environment and the Django dev environment were running in separate docker containers, so localhost was referring to the node container, not the bridged network.

因此关键是要使用容器链接,该链接在使用docker-compose时会自动创建,并将其用作主机名.所以我将其更改为

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

"proxy": {
    "/api":  {
        "target": "http://django:8000"
    }
},

这是可行的,只要您使用相同的docker-compose命令启动两个容器,否则就必须在docker-compose.yml文件中手动指定external_links.

And that worked, as long as you launch both containers with the same docker-compose command, otherwise you have to manually specify external_links in your docker-compose.yml file.

这篇关于反应代理错误:无法将请求/api/从localhost:3000代理到http://localhost:8000(ECONNREFUSED)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆