Nodejs Http Proxy Server Gives Socket Hang up Error Inside Docker Container

I have created a containerized proxy server (say worker server) that listens on port 5000. I ran the container with --network=host option so that it may access the ports on host machine without needing the -p flag.The server has an endpoint get-buttons . The endpoint executes a shell script and then forwards the request to port 6000 of host machine with the same endpoint. I have used http-proxy of nodejs. The code is given below:

const express = require('express');
const httpProxy = require('http-proxy');
const { exec, execSync } = require('child_process');

const app = express();
const proxy = httpProxy.createProxyServer({});

app.get('/get-buttons', (req, res) => {
  // Execute a shell script
  exec(`sh hello_world.sh ${req.query.abc}`, (error, stdout, stderr) => {
    if (error) {
      console.error(`Error starting engine: ${error}`);
      res.status(500).send('Error executing shell script');
      return;
    }
    console.log('Shell script executed successfully');
    proxy.web(req, res, { target: 'http://localhost:6000/get-buttons' }, function (error) {
      console.log("Proxy Error ", error)
    });
  });
});

app.listen(5000, () => {
  console.log('Worker server is running on port 5000');
});

I get the following error every time:

Error: socket hang up
    at connResetException (internal/errors.js:639:14)
    at Socket.socketOnEnd (_http_client.js:499:23)
    at Socket.emit (events.js:412:35)
    at endReadableNT (internal/streams/readable.js:1333:12)
    at processTicksAndRejections (internal/process/task_queues.js:82:21) {
  code: 'ECONNRESET

It may be noted that, if I run curl localhost:6000/get-buttons from inside the container, it gives me a response. I am confused, if the curl command works,why the nodejs proxy giving me a socker hang up error.

You need to understand that http clearly differentiates between host and path. When using a proxy server, the host is changed, but the path is not (unless specifically configured).

You are trying to set a target with host and path, I don’t think that works. (Doc)

You can check host machine firewall rules if the port is allowed, also try to use ip address instead of localhost.

You mean using 127.0.0.1 instead of localhost?

Yes or container network ip address

Hi,

What you meant is that I should use http://localhost:6000/ instead of http://localhost:6000/get-buttons, right?