React Proxy error: Could not proxy request /api/ from localhost:3000 to http://localhost:8000 (ECONNREFUSED)

前端 未结 6 1456
忘了有多久
忘了有多久 2021-01-17 08:02

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 r

6条回答
  •  没有蜡笔的小新
    2021-01-17 08:19

    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.

    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"
        }
    },
    

    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.

提交回复
热议问题