webpack-dev-server icon indicating copy to clipboard operation
webpack-dev-server copied to clipboard

devServer proxy shows the pass happening in the console, but doesn't work in browser

Open chris-schmitz opened this issue 7 years ago • 4 comments

  • Operating System: Mac OS 10.14
  • Node Version: 10.2.1
  • NPM Version: 5.6.0
  • webpack Version: 4.17.1
  • webpack-dev-server Version: 3.1.14
  • [x] This is a bug
  • [ ] This is a modification request

Code

  // webpack.config.js
    devServer: {
        contentBase: dest,
        inline: true,
        proxy: {
            "/api/v1/**": {
                target: "http://localhost:3000/api/v1",
                logLevel: "debug"
            }
        }
    },
  // additional code, remove if not needed.
axios
    .get("/api/v1/test")
    .then(response => {
        console.log(response);
    })
    .catch(error => {
        console.error(error);
    });

Expected Behavior

When the axios request is made from the browser I expect the API request to be passed to the node/express server running on port 3000 via the webpack devserver proxy and then returned to the browser.

Actual Behavior

The request gets proxied, I can see it in the dev server output in the console:

screen shot 2019-01-07 at 12 07 11 pm

But in the browser I get a 404 error:

screen shot 2019-01-07 at 12 08 22 pm

And I can confirm the API route returns successfully on it's own:

screen shot 2019-01-07 at 12 12 45 pm

I've tried altering my proxy settings per responses to similar github issue threads, but I can't seem to get it to work. The biggest difference I've seen in my issue and others online is that I can see the proxy pass happening in the console.

UPDATE

After typing this out and re-reading it, I notice that the path sections after the /api/v1 are not being passed in the proxy pass, e.g.

GET /api/v1/test -> http://localhost:3000/api/v1/ <-- test is not being passed through

The thing is, I have a json object that is passed back from requests going to api/v1 as well, and that is not being passed back either.

For Bugs; How can we reproduce the behavior?

Set up a project via the vue-cli, add the proxy config noted above, create a node-express server with the path:

app.get("/api/v1/test", (request, response) => {
    response.json({ test: "worked" });
});

chris-schmitz avatar Jan 07 '19 18:01 chris-schmitz

Maybe port missmatch 8080 and 3000?

How so? If I'm understanding correctly, the webpack dev server should be hosting from port 8080 for my front end code base and any requests to :8080/api/v1/... are passed to :3000/api/v1/... via the proxy. That should be what's happening, right?

Both the webpack dev server's code base and the express server are working from their respective port, it's just the proxy from one to the other and back that doesn't seem to be working.

chris-schmitz avatar Jan 07 '19 20:01 chris-schmitz

@chris-schmitz can you create full minimum reproducible test repo and describe in readme what you actually have and what you expected?

alexander-akait avatar Jan 09 '19 09:01 alexander-akait

Closing due to inactivity. Please test with latest version and feel free to reopen if still regressions. Thanks!

alexander-akait avatar Mar 21 '19 17:03 alexander-akait

I have the same issue. Did you solve the problem?

yudi704 avatar Jul 17 '19 06:07 yudi704