Error occured while trying to proxy to: localhost:3000/api on a M1 Macbook
Asked Answered
A

2

8

I am working on a project with Node.js, React.js and MongoDB.

When I send request to server, I get the following error:

Error occurred while trying to proxy request /api/auth/login from localhost:3000 to http://localhost:6000 (ECONNRESET).

I have my client running at port 3000, server at port 6000 locally. Here is the client side proxy middleware setup code:

const proxy = require("http-proxy-middleware");

module.exports = function(app) {
  app.use(proxy("/api/", { target: "http://localhost:6000", "secure": "false" }));
};

I have tried using 127.0.0.1 inplace of localhost, but didn't work.

The project works fine in Windows laptop. But, it is having problem with M1 Mac.

Any guidance would be of great help to me.

Aggiornamento answered 26/4, 2022 at 2:50 Comment(2)
Is the server actually running and bound on port 6000?Serology
@Serology Yes, the server is listening on port 6000. When I request the server I get this res: node:events:504 [0] throw er; // Unhandled 'error' event [0] Error: read ECONNRESET [0] at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) [0] Emitted 'error' event on ClientRequest instance at: [0] at TLSSocket.socketErrorListener (node:_http_client:442:9) [0] at emitErrorNT (node:internal/streams/destroy:157:8) [0] at processTicksAndRejections (node:internal/process/task_queues:83:21) { [0] errno: -54, [0] code: 'ECONNRESET', [0] syscall: 'read'}Aggiornamento
A
5

I changed the version of Node.js to 14.9.0 and it worked.

These are the solutions found in the internet that didn't work for me:

  • Changing node.js version to other stable version 16 or 18
  • specifying an IPv4 address like this on server (because I can see my server was running on IPv6): server.listen(13882, "0.0.0.0", function() { });
  • Removing proxy entry from the Package.json file
  • Updating to {target: "http://localhost:6000/"} OR {target: "https://localhost:6000/"} OR {target: "http://127.0.0.1:6000"} OR {'http://[::1]:6000'} OR {app.use(proxy("/api/", {target:"http://localhost:6000",secure: false,changeOrigin: true}));}

I have this entry in package.json file "proxy": "http://localhost:6000"

This is my setupProxy.js file

const proxy = require("http-proxy-middleware");

module.exports = function(app) {
  app.use(proxy("/api/", { target: "http://localhost:6000" }));
};
Aggiornamento answered 29/4, 2022 at 4:51 Comment(0)
B
16

I got the same error using M1.

This code started working correctly for me.

http://localhost:3000/ -> http://127.0.0.1:3000/

server.js

"use strict";

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

const PORT = 9090;
const HOST = "0.0.0.0";

const app = express();

app.use(
  createProxyMiddleware("/", {
    target: "http://127.0.0.1:3000/",
  })
);

app.listen(PORT, HOST);

packege.json


{
  "name": "web",
  "version": "1.0.8",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.18.2",
    "http-proxy-middleware": "^2.0.6"
  }
}
  • node v18.11.0
  • npm 8.19.2

Server at "http://127.0.0.1:3000/" - default configuration for create-react-app ("react-scripts": "^5.0.1")

Bowne answered 16/11, 2022 at 17:11 Comment(0)
A
5

I changed the version of Node.js to 14.9.0 and it worked.

These are the solutions found in the internet that didn't work for me:

  • Changing node.js version to other stable version 16 or 18
  • specifying an IPv4 address like this on server (because I can see my server was running on IPv6): server.listen(13882, "0.0.0.0", function() { });
  • Removing proxy entry from the Package.json file
  • Updating to {target: "http://localhost:6000/"} OR {target: "https://localhost:6000/"} OR {target: "http://127.0.0.1:6000"} OR {'http://[::1]:6000'} OR {app.use(proxy("/api/", {target:"http://localhost:6000",secure: false,changeOrigin: true}));}

I have this entry in package.json file "proxy": "http://localhost:6000"

This is my setupProxy.js file

const proxy = require("http-proxy-middleware");

module.exports = function(app) {
  app.use(proxy("/api/", { target: "http://localhost:6000" }));
};
Aggiornamento answered 29/4, 2022 at 4:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.