-
Notifications
You must be signed in to change notification settings - Fork 840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do Not Proxy Request When Custom Router Throws Error #411
Comments
Currently it is not possible. How do you use the http-proxy-middleware? Via webpack-dev-server (create-react-app, angular-cli, vue-cli...)? Or do you mount it in your own express or express-like server? |
This is in an express server. Looking through the code in prepareProxyRequest, it seems like there should be some error handling there to catch errors. The two error return types I can think of for a custom router causes two separate issues.
|
You could use express's error handling: https://expressjs.com/en/guide/error-handling.html Basically import * as express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';
const app = express();
const proxy = createProxyMiddleware({
// target is a required parameter
target: 'do-not-use',
router: (req) => {
try {
...
return target;
}
catch (err) {
throw err;
}
},
});
app.use(proxy);
app.use((err, req, res, next) => {
res.send(500, 'oops some routing went wrong')
}); |
I tried this out, but I am still getting UnhandledPromiseRejection errors logged to the console. Should the code that calls prepareProxyRequest catch the error and then pass it to the next() function? try {
const activeProxyOptions = await this.prepareProxyRequest(req);
this.proxy.web(req, res, activeProxyOptions);
}
catch(err) {
next(err);
} |
Was hoping that Your suggestion to solve it makes sense than. Do you mind creating PR with your suggestion to fix this issue? |
I am trying to take complete control of the target routing. I would like the proxied request to be aborted before the client socket is opened if my router was unable to determine a target host. Is this possible to do?
The current http-proxy code will still use the original target "do-not-use". I would rather the proxy just abort the request entirely if the router function does not return a target.
The text was updated successfully, but these errors were encountered: