Search code examples
node.jsexpressproxynext.jseconnrefused

Error occurred while proxying request localhost:3000/api/product to http://localhost:5000/


We are trying to create an simple eCommerce app using Typescript, express and nextjs.

When we make a request to it, it throws the following error.

[HPM] Error occurred while proxying request localhost:3000/api/product to http://localhost:5000/ [ECONNREFUSED] (https://nodejs.org/api/errors.html#errors_common_system_errors)

My friend is using Windows, and code is working on his PC but not on my Ubuntu desktop.

I tried killing the port like below. It also kills the port but this does not help.

$ sudo lsof -t -i:5000
6033
$sudo kill -9 6033

$sudo lsof -t -i:3000
6101
$sudo kill -9 6101

Proxy server is listening at server.js file as:

const express = require('express');
const next = require('next');
const { createProxyMiddleware } = require('http-proxy-middleware');

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

app
  .prepare()
  .then(() => {
    const server = express();
    // apply proxy in dev mode
    if (dev) {
      server.use(
        '/api',
        createProxyMiddleware({
          target: 'http://localhost:5000',
          changeOrigin: true,
        })
      );
    }

    server.all('*', (req, res) => {
      return handle(req, res);
    });

    server.listen(3000, (err) => {
      if (err) throw err;
      console.log('> Ready on http://localhost:5000');
    });
  })
  .catch((err) => {
    console.log('Error', err);
  });

Solution

  • Found the solution.

    I have ignore node_module and upload folder in .gitignore

    I forgot to add the folder after cloning. So, it was showing proxy error.

    After I add upload folder error was resolved.