Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localhost didn’t send any data. ERR_EMPTY_RESPONSE nodejs

Tags:

node.js

docker

I'm trying to convert this app to docker but I'm getting this error.

Here's my express.js file

const express = require("express");
const app = express();
const portNumber = 3000;
const sourceDir = "dist";
const expressStaticGzip = require("express-static-gzip");
app.use(
  "/",
  expressStaticGzip(sourceDir, {
    enableBrotli: true,
    orderPreference: ["br", "gz"],
    setHeaders: function(res, path) {
      res.setHeader("Cache-Control", "public, max-age=31536000");
    }
  })
);

app.listen(portNumber, () => {
  console.log(`Express web server started: http://localhost:${portNumber}`);
  console.log(`Serving content from /${sourceDir}/`);
});

here's my dockerfile

FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "node", "express.js" ]

Help is appreciated.

like image 752
Hassan Avatar asked Apr 15 '26 18:04

Hassan


2 Answers

This is a very common error. When you're broadcasting within a docker container you usually have to listen on host 0.0.0.0 not just localhost. If you listen on localhost docker won't be able to route traffic from outside of the container to the loopback interface within the container.

You can fix this by updating your express code to listen on 0.0.0.0. I believe that's this:

app.listen(portNumber, "0.0.0.0", () => {
  console.log(`Express web server started: http://0.0.0.0:${portNumber}`);
  console.log(`Serving content from /${sourceDir}/`);
});

You'll then need to run the dockerfile with something like:

docker build -t node-app .
docker run -p 3000:3000 node-app
like image 185
maxm Avatar answered Apr 18 '26 10:04

maxm


I was running the angular application using the command ng serve and facing the same problem.

Because the live development server listens to http://localhost:4200 by default.

than I used the command ng serve --host 0.0.0.0 to run the angular project and that solved my problem.

like image 40
Ali Shan Avatar answered Apr 18 '26 08:04

Ali Shan