-
Notifications
You must be signed in to change notification settings - Fork 835
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
Handle async pathRewrite function #374
Comments
same requestion! |
Workaround: I ended up just using the underlying http-proxy lib. const http = require("http");
const httpProxy = require("http-proxy");
const url = require("url");
const proxy = httpProxy.createProxyServer({ target: "http://example.com" });
const server = http.createServer( /* handle HTTP requests */ );
server.on("upgrade", async (req, socket, head) => {
const parsedUrl = url.parse(req.url, true /* also parse query params */);
if (parsedUrl.pathname === "/api/foo") {
// this should be in a try/catch for any situation in which the Promise could
// reject
const usr = await someAuthService.validate(parsedUrl.query.auth_token);
const someVar = await someDbService.getField1(usr);
const otherVar = await someDbService.getField2(usr);
// Now do the path rewrite
req.url = `/other-api/bar?db-field-1=${someVar}&db-field2=${otherVar}`;
// let request through
proxy.ws(req, socket, head);
} else {
// Invalid path, close the WebSocket
socket.destroy();
// Alternatively, could choose some other path rewrite, like:
// req.url = "/other-api/404";
// proxy.ws(req, socket, head);
}
});
server.listen(3000); |
Merged
I also had a need for this (in my case, the path rewriting behavior is dependent on its own HTTP request, so |
published Thanks @rsethc ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this a feature request?
Yes. I would like a
pathRewrite
function that returns aPromise
to be handled. Currently it can only return astring
.My use case is: when a request for
/api/foo?auth_token=xxx
arrives, I need to:auth_token
and check its validity. This is an async operation/other-api/bar?db-field-1=abc&db-field-2=abc
. If it did not go well, the path will be rewritten to/other-api/404
Similar to #318, but this covers the case where more than just the response headers/status needs to be modified.
I have this working and tested in a local fork, please let me know if you would accept a PR.
The text was updated successfully, but these errors were encountered: