Home > database >  Dockerized React app axios req to Backend doesn't work
Dockerized React app axios req to Backend doesn't work

Time:01-20

I have 2 containers:

  • nginx(serving React app)
  • node

Both inside the same docker-compose file.

When i run it locally ('npm start' for React and node index.js for backend) they work fine (backend receive request) - but when they are both in containers, something goes wrong..

** when i exec the nginx container - it does recognize http://server:5000/ endpoint (with curl)

docker-compose.yaml

version: "3.8"
services:
    server:
        build:
            context: ./server
        ports:
            - "5000:5000"
        networks:
          - frontend
    nginx:
        build:
            context: ./app
        restart: always
        ports:
            - "80:80"
        networks:
          - frontend
          
networks:
  frontend:

request part on frontend

async function sendMsg(...order){
    await axios.put('http://server:5000/msg', order)
}

receive part on backend

app.put('/msg', (req, res) => {
  console.log(req.body)
}

CodePudding user response:

The problem is your react app is served by your container but is executing in your browser.

Your browser doesn't know what "server" refers to.

Try changing http://server:5000/msg to http://localhost:5000/msg

Also make sure your node server is running.

  •  Tags:  
  • Related