Http-proxy-middleware: Consider making target optional

Created on 4 Dec 2018  路  4Comments  路  Source: chimurai/http-proxy-middleware

Is this a feature request?

Yes

For some routes, I just want them to return a 404, and I don't want to proxy them anywhere. I'm able to accomplish this using onProxyReq like so:

proxy('/route', {
    target: 'https://localhost/', // target is ignored since we always send 404s
    onProxyReq: (proxyReq, req, res) => { res.status(404).send(); }
}));

For these routes, the target really is irrelevant - there's no place I'm trying to proxy them to. But, I have to include a target in the config, or else I'll get the following error:

[HPM] Missing "target" option. Example: {target: "http://www.example.org"}

Maybe target should be optional, and the message above could be a warning instead of an error.

Most helpful comment

its still relevant when using custom router.. in this case no need "target"

All 4 comments

In the use-case you describe; the whole proxy is not needed right?

With just express middleware:

app.use('/route', (req, res, next) => {
  res.status(404).send();
})

instead of:

app.use(proxy('/route', {
    target: 'https://localhost/', // target is ignored since we always send 404s
    onProxyReq: (proxyReq, req, res) => { res.status(404).send(); }
}));
)

Yeah you're absolutely right! Closing.

its still relevant when using custom router.. in this case no need "target"

@chimurai could we maybe reconsider making target optional? Just stumbled upon this when using the router option to dynamically determine the target.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pwlerke picture pwlerke  路  6Comments

Jack-Works picture Jack-Works  路  8Comments

maks-rafalko picture maks-rafalko  路  8Comments

joshiste picture joshiste  路  4Comments

minhduchcm picture minhduchcm  路  7Comments