简体   繁体   中英

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

Developing a Gatsby App using this Starter https://github.com/the-road-to-react-with-firebase/react-gatsby-firebase-authentication

I keep getting this HPM Error after updated my node packages when I try to visit my page after running Gatsby Develop. The project compiles successfully but then I get this error in the browser and nothing shows up.

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

and this in the terminal:

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

once I remove this from the gatsby-config.js file it works and the pages generated in the browser:

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

However, I then get this error in the terminal:

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

I want to know why isn't the Proxy working and what is the above module exports really doing anyway. I feel like this workaround I'm doing isn't good. Any help or advice would be great!!

Github Repo:

GitHub Repo for The project

That error means there's nothing running at http://localhost:4000 . There seems to be a few problem with your setup:

First, your developMiddleware setup points to http://localhost:4000 , but your server ( server.js ) by default runs at http://localhost:3000 . Perhaps you forgot to startup the server, or start it up at the wrong port?

Second, if I read it correctly,in your proxy middleware, you're proxying every route to port 4000? This will render Gatsby useless. Here's an example of a better proxy setup:

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

With this, only request to localhost:8000/api will be proxied to localhost:4000 .

Hope it helps!

I did have same error ECONNREFUSED while trying to debug both Angular and Node.js with VS Code and WSL2 Ubuntu-20.04 environment. Apparently that was solved by adding deprecated useWSL flag

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

I had the same error. In my case, I forgot to run npm run server first. This will start the server.

Then run npm start in another terminal window.

npm start will call "ng serve --proxy-config./proxy.json"

My proxy.json file:

{
  "/api": {
    "target": "http://localhost:9000",
    "secure": false
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM