【问题标题】:Error occured while trying to proxy to: localhost:3000/api on a M1 Macbook尝试代理时发生错误:M1 Macbook 上的 localhost:3000/api
【发布时间】:2022-12-16 12:09:57
【问题描述】:

我正在使用 Node.js、React.js 和 MongoDB 开发一个项目。

当我向服务器发送请求时,出现以下错误:

尝试将请求 /api/auth/loginlocalhost:3000 代理到 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 上有问题。

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

【问题讨论】:

  • 服务器是否实际运行并绑定在端口 6000 上?
  • @Phil 是的,服务器正在侦听端口 6000。当我请求服务器时,我得到了这个 res: node:events:504 [0] throw er; // 未处理的“错误”事件 [0] 错误:在 TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) [0] 处读取 ECONNRESET [0] [0] 在 ClientRequest 实例上发出的“错误”事件:[0] 在 TLSSocket .socketErrorListener (node:_http_client:442:9) [0] 在 emitErrorNT (node:internal/streams/destroy:157:8) [0] 在 processTicksAndRejections (node:internal/process/task_queues:83:21) { [0 ] 错误号:-54,[0] 代码:'ECONNRESET',[0] 系统调用:'读取'}

标签: node.js reactjs apple-m1


【解决方案1】:

我将 Node.js 的版本更改为 14.9.0 并且它有效。

这些是在互联网上找到的对我不起作用的解决方案:

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

我在 package.json 文件中有这个条目"proxy": "http://localhost:6000"

这是我的 setupProxy.js 文件

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

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

【讨论】:

    【解决方案2】:

    我在使用 M1 时遇到了同样的错误。

    此代码开始为我正常工作。

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

    服务器.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);
    
    

    包.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.0
    • npm 8.19.2

    服务器位于“http://127.0.0.1:3000/”——create-react-app的默认配置(“react-scripts”:“^5.0.1”)

    【讨论】:

      【解决方案3】:

      仍然无法正常工作,尝试了上面的所有建议。 在 macbook pro 2019 上运行,降级到节点 14.9。

      还有其他方法可以检查或调试我在哪里做错了吗?

      // api gateway
      import { Router } from 'express'
      import { createProxyMiddleware, Filter, Options, RequestHandler } from 'http-proxy-middleware';
      const router: Router = Router()
      
      const AUTH_PROXY_OPT = {
        // protocol: 'https',
        target: 'http://127.0.0.1:9999',
        changeOrigin: false,
        pathRewrite: {
          '^/v1/auth': '/'
        },
        proxyTimeout: 12000,
        headers: {
          "Connection": "keep-alive"
        },
        logger: console,
      }
      
      router.use(createProxyMiddleware(AUTH_PROXY_OPT))
      
      export default router

      【讨论】:

        猜你喜欢
        • 2018-11-26
        • 1970-01-01
        • 1970-01-01
        • 2019-07-09
        • 2021-12-20
        • 1970-01-01
        • 2022-01-26
        • 2019-11-11
        • 2022-01-03
        相关资源
        最近更新 更多