Skip to content

Some errors are not handled correctly for proxied requests #2380

Closed
@OliverJAsh

Description

@OliverJAsh
  • Operating System: macOS 10.15.2
  • Node Version: 12.8.1
  • NPM Version: 6.10.2
  • webpack Version: see package.json below
  • webpack-dev-server Version: see package.json below
  • Browser: N/A
  • This is a bug
  • This is a modification request

Code

// webpack.config.js
module.exports = {
    devServer: {
        port: 4000,
        proxy: {
            '/api': 'http://localhost:3000'
        }
    }
}
// index.js
const express = require("express");

const app = express();

app.get("*", (req, res) => {
  res.send("foo");
});

// error handling middleware
app.use((error, req, res, next) => {
  res.send("my custom error page");
});

app.listen(3000);
// package.json
{
  "dependencies": {
    "express": "^4.17.1",
    "webpack": "^4.41.5",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.10.1"
  }
}

Expected Behavior

  1. Run node index.js
  2. Run webpack-dev-server
  3. curl "localhost:4000/api/%"

The response should be "my custom error page", indicating that the Express error handling middleware for the API server was used.

Actual Behavior

The response is URIError: Failed to decode param …, indicating that the Express error handling middleware for the API server was not used.

I would not expect webpack-dev-server to change how errors are handled for proxied requests.

Technical notes

I played around with using http-proxy-middleware on its own to see if the root issue was inside there, but using the following code, I could not reproduce this issue:

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

{
  const app = express();

  app.get("*", (req, res) => {
    res.send("foo");
  });

  app.use((error, req, res, next) => {
    res.send("my custom error page");
  });

  app.listen(3000);
}

{
  const app = express();

  app.use(httpProxyMiddleware({ target: "http://localhost:3000" }));

  app.listen(4000);
}

For Bugs; How can we reproduce the behavior?

See above

For Features; What is the motivation and/or use-case for the feature?

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions