score:29

Accepted answer

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.

score:0

the correct answer will be to use manual proxy with

  1. target = docker address django:4000
  2. correct host header localhost:8000

because if django uses reverse function which returns absolute url

reverse('preview-mail', args=[mail.pk],request=request)

you need to have correct host header for it, or you may get the result url like https://django:4000/your-url`

const proxy = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(
    proxy('/api', {
      target: 'http://django:4000',
      changeorigin: true,
      secure: false,
      pathrewrite: {
        '^/api': ''
      },
      onproxyreq: function (proxyreq, req, res) {
          proxyreq.setheader("host", 'localhost:8000')
      }
    })
  )
}

score:0

if you're working on a mern stack app, make sure you're not in the client folder. you need to be in the root. while in the root, run this command in the terminal. npm run start:dev

score:1

could see the error after upgrading yesterday docker to version v19.03.13 (on mac), restarting docker fixed the issue. the application also runs node.js/react, but not django. basically, i had issues with connection to mongodb atlas related to authentication/fetching anything from the cloud database.

score:1

choosing the exact value for localhost to populate the "target" property is mostly the solution (it can be localhost, 127.0.0.1, [::1] ).

a mac user should type in terminal to get the solution:

sudo lsof -itcp -stcp:listen -n -p

score:2

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

score:2

didn't actually get the answer here i was looking for but had an alternative solution work for me. i think it's specifically related to node v17, as that's when it started happening for me but the solution was pretty simple.

i updated:

"proxy": "http://localhost:8000"

to:

"proxy": "http://127.0.0.1:8000"

in case it's relevant (i don't think it is) - i was proxying to a django server.

score:3

if you don't feel like setting up docker compose, you can also use a docker network:

create network and run docker containers within that network

docker network create webapp_network

docker run -d -p 5000:5000 --name webapp_backend --network webapp_network webapp_backend_image

docker run -d -p 3000:3000 --name webapp_frontend --network webapp_network webapp_frontend_image

added a line in package.json of my frontend react webapp:

"proxy": "http://webapp_backend:5000"

note you can now refer to your backend using the container name instead of localhost

score:11

i faced a similar issue but in mac machine. i changed localhost to 127.0.0.1 in the package.json and that worked for me as below:

"proxy": "http://127.0.0.1:5000"

score:15

i'm running into the same problem as well. most search results mention adding "secure": false or "ignorepath": true to your proxy config. something like this:

"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:3000 and http://localhost:8000) work completely fine in the browser, maybe since the container is actually proxying it needs to use a docker address?

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:

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

Related Query

More Query from same tag