Gatsby 错误 [HPM] 尝试代理请求时发生错误 / 从 localhost:8000 到 http://localhost:4000 (ECONNREFUSED)

Gatsby error [HPM] Error occurred while trying to proxy request / from localhost:8000 to http://localhost:4000 (ECONNREFUSED)

使用此 Starter 开发 Gatsby 应用程序 https://github.com/the-road-to-react-with-firebase/react-gatsby-firebase-authentication

在 运行 Gatsby Develop 之后尝试访问我的页面时,我在更新我的节点包后不断收到此 HPM 错误。该项目编译成功,但随后我在浏览器中收到此错误并且没有任何显示。

error occurred while trying to proxy to: localhost:8000/

终端中的这个:

error [HPM] Error occurred while trying to proxy request / from localhost:8000 to http://localhost:4000 (ECONNREFUSED

一旦我从 gatsby-config.js 运行的文件和在浏览器中生成的页面:

module.exports = {
    developMiddleware: app => {
        app.use(
            proxy({
                target: "http://localhost:4000",
            })
        )
    },
}

但是,我在终端中收到此错误:

Error loading a result for the page query in "/404.html". The query was not run and no cached result was found. Page not found /404.html

我想知道为什么代理不工作以及上面的模块导出到底在做什么。我觉得我正在做的这个解决方法不好。任何帮助或建议都会很棒!!

Github 回购:

GitHub Repo for The project

该错误意味着 http://localhost:4000 上没有任何内容 运行。您的设置似乎有一些问题:

首先,您的 developMiddleware 设置指向 http://localhost:4000,但您的服务器 (server.js) 默认运行在 http://localhost:3000。也许您忘记启动服务器,或者在错误的端口启动它?

其次,如果我没看错的话,在你的代理中间件中,你正在代理到端口 4000 的每条路由?这将使盖茨比变得无用。这是一个更好的代理设置示例:

module.exports = {
  developMiddleware: app => {
    app.use(
      "/api",
      proxy({
        target: "http://localhost:4000",
      })
    )
  },
}

有了这个,只有 localhost:8000/api 的请求会被代理到 localhost:4000

希望对您有所帮助!

我在尝试使用 VS Code 和 WSL2 Ubuntu-20.04 环境调试 Angular 和 Node.js 时确实遇到了同样的错误 ECONNREFUSED。显然,这是通过添加已弃用的 useWSL 标志

解决的
{
  "type": "node",
  "request": "launch",
  "name": "Launch Node Express via NPM",
  "runtimeExecutable": "npm",
  "useWSL": true,
  "runtimeArgs": ["run-script", "node:debug"],
  "port": 9229
},