试图代理到:Localhost:M1 MacBook上的3000/API时发生错误

发布于 2025-01-24 03:09:47 字数 639 浏览 3 评论 0原文

我正在使用Node.js,React.js和MongoDB进行一个项目。

当我将请求发送到服务器时,我会收到以下错误:

尝试代理请求/api/auth/login/login/code> localhost:3000 to http:// http:// localhost:6000(( econnreset)。

我的客户端在端口3000(端口6000)运行。这是客户端代理中间件设置代码:

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

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

我尝试使用127.0.0.1 localhost的内置,但无效。

该项目在Windows笔记本电脑中正常工作。但是,M1 Mac有问题。

任何指导都会对我有很大的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白馒头 2025-01-31 03:09:47

我使用M1遇到了相同的错误。

该代码开始适合我工作。

http:// localhost:3000/ - > http://127.0.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"
  }
}
  • 节点v18.11.11.11.0
  • npm 8.19.2

服务器在“ http://127.0.0.0.0.1.1:3000/” a href =“ https://create-react-app.dev/” rel =“ noreferrer”> create-react-app-app-app (“ active-scripts”:“:“^5.0.1”)

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")

书间行客 2025-01-31 03:09:47

我将Node.js的版本更改为14.9.0,并且可以使用。

这些是互联网上发现的解决方案,对我不起作用:

  • 将Node.js版本更改为其他稳定版本16或18
  • 在服务器上指定这样的IPv4地址(因为我可以看到我的服务器在IPv6上运行):server.listen(13882,“ 0.0.0.0.0”,function(function(){});>
  • 从package.json文件中删除代理条目
  • 更新到{target:“ http:// localhost:6000/“}{target:” https:// localhost:6000/“} or <代码> {目标:“ http://127.0.0.1:6000”} 或
    {'http:// [:: 1]:6000'}{app.use(proxy(“/api/”,{target:target:“ http:// localhost:6000) “,secure:false,tracgerorigin:true}));}

我在package.json文件“代理”中具有此条目:“ http:// localhost:6000”

这是我的setUpproxy.js文件

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

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

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" }));
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文