Home > Software engineering >  NodeJs/ Espress / NestJS server will not start if request is made when starting
NodeJs/ Espress / NestJS server will not start if request is made when starting

Time:10-18

I have a server running Nestjs and Express on Nodejs.

If I make any request while the server is starting I get the following error and it fails to start:

Waiting for the debugger to disconnect...
/node_modules/http-proxy/lib/http-proxy/index.js:120
    throw err;
    ^

Error: connect ECONNREFUSED ::1:61903
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16)
    at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 61903
}

The error is thrown at this point in /node_modules/http-proxy/lib/http-proxy/index.js:

ProxyServer.prototype.onError = function (err) {
  //
  // Remark: Replicate node core behavior using EE3
  // so we force people to handle their own errors
  //
  if(this.listeners('error').length === 1) {
    throw err;
  }
};

I am starting the server with

 const task = SERVICE_TASK.toUpperCase();
  const app = await createApp(task);
  app.set('trust proxy', true);
  app.enableShutdownHooks();
  const {httpAdapter} = app.get(HttpAdapterHost);
  app.useGlobalFilters(new ExceptionLogger(httpAdapter));
  app.enableCors({origin: '*'});
  await app.listen(PORT);

How do I handle these errors?

CodePudding user response:

Added the following code before the http-proxy was created.

import proxy from 'http-proxy';
proxy.prototype.onError = function (err) {
    if(this.listeners('error').length === 1) {
        console.warn(err);
    //   throw err;
    }
};
  • Related