You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was also looking at making external calls to add/verify authorization headers but came to the same conclusion. I'm guessing that a viable option is to have another middleware do the external call and have it mutate incoming request headers (would transfer over to onProxyReq). But it would be really nice to be able to do this within http-proxy-middleware having matched the route/pattern of the incoming request.
Looks like there is some demand for async listeners.
I'll consider implementation after some investigation.
@julmot Can you point to the documented node-http-proxy example?
@magnusp Think you can to achieve the same result by chaining middlewares.
Assuming you use express you can write a authValidator and mount it in express like: app.use('/some/endpoint', authValidator, proxy)
Reacted by Paweł Szczerbicki, Kin Ueng and juventus18
Im using it via webpack-dev-server which has a configuration property for http-proxy-middleware. The devserver also has a way to setup additional middleware via the "before" and "after" configuration properties. This leaves me having a choice of using my middleware+http-proxy-middleware in a "before" or only setting up my middleware in "before" and using the dedicated "proxy" configuration property. Im mostly mentioning this here for other people stumbling upon the issue and letting them see the options available. But async listeners is an awesome feature. I remember wanting it when implementing a chaos monkey where delays on the proxy were introduced randomly and configurably, at runtime.
@chimurai I have to deal with promises in both proxyReq and proxyRes functions. Would be very helpful to have to have these functions support promises.
Reacted by Edwin, Kavita Chaudhry, Claas Ahlrichs, Baptiste Vrignaud, Ramrami Mohamed, MrrrLuiigii, Hari, Robin Wilborn, Adel Hamad, Owen Schwartz and 3 more
Reacted by bforbis, Andrew Stout, Andrew Moon, Fredrik Nicol, Plamen Dzhorev, Denny Lim, Aleksandr N. Ryzhov, Atanas Rusenov, xjshuang and Mateusz Turcza
Seems reasonable. I'm guessing that the proxyReq.socket should probably be aborted when an error occurs otherwise it could be left hanging until aborted by an timeout in either end.
@darewreck54 I was having this issue too, and after looking into the source code, at least for me, I couldn't find any way to circumvent this situation, even when using req.headers[customHeader].
But having custom header is essential to the functioning of my application. So finally I decided to change the whole architecture to microservice-based. Sorry, couldn't be much of your help.
If you don't need custom header for your application to be functional, the hack I proposed earlier is still a feasible way to get around the original issue.
Using req.headers[customHeader] won't give you that annoying error, but the header won't reach the upstream server anyway.
I actually end up creating my own express middleware that execute the promise and when its successful it will trigger next(). I'll store the updated header in the request and send it to the proxy middleware
There was a PR last year on http-proxy for this. And without async support in handlers even though it might be somewhat trivial to implement, I didnt really get a warm fuzzy when I looked at the internals.
Ill just use a library with keep alive and call it good.
So i have this setup where i can still alter the response header AFTER i get the response from the proxied endpoint and do async stuff. In this sample i also included the fake server im mocking to (3001). Maybe nice for your recipe? Its setting response header on the response AFTER getting the response and do async stuff.
Just a note to anyone else looking - the posted workaround which pauses the socket seems to not work with a Websocket upgrade.
Edit:
I did find a workaround here - handle the server's upgrade and call proxy.upgrade from your async handler:
constserver=expressApp.listen(8080);server.on('upgrade',async(req: Request,socket: Socket,head: Buffer)=>{someAsyncFunction().then(()=>{// do what you need with the results of the async function hereif(upstreamProxy.upgrade){upstreamProxy.upgrade(req,socket,head);}});});
Activity
julkue commentedon Jan 24, 2019
@pawelszczerbicki Having the same question. I will probably use node-http-proxy instead, as there's a documented example for this case.
magnusp commentedon Feb 18, 2019
I was also looking at making external calls to add/verify authorization headers but came to the same conclusion. I'm guessing that a viable option is to have another middleware do the external call and have it mutate incoming request headers (would transfer over to onProxyReq). But it would be really nice to be able to do this within http-proxy-middleware having matched the route/pattern of the incoming request.
chimurai commentedon Feb 19, 2019
Looks like there is some demand for async listeners.
I'll consider implementation after some investigation.
@julmot Can you point to the documented node-http-proxy example?
@magnusp Think you can to achieve the same result by chaining middlewares.
Assuming you use
expressyou can write aauthValidatorand mount it in express like:app.use('/some/endpoint', authValidator, proxy)magnusp commentedon Feb 19, 2019
Im using it via webpack-dev-server which has a configuration property for http-proxy-middleware. The devserver also has a way to setup additional middleware via the "before" and "after" configuration properties. This leaves me having a choice of using my middleware+http-proxy-middleware in a "before" or only setting up my middleware in "before" and using the dedicated "proxy" configuration property. Im mostly mentioning this here for other people stumbling upon the issue and letting them see the options available. But async listeners is an awesome feature. I remember wanting it when implementing a chaos monkey where delays on the proxy were introduced randomly and configurably, at runtime.
SreeBS commentedon Feb 20, 2019
@chimurai I have to deal with promises in both proxyReq and proxyRes functions. Would be very helpful to have to have these functions support promises.
montera82 commentedon Feb 28, 2019
+1 one for async
how can I achieve using the result of an async call there?
i am doing something like this
shian15810 commentedon Mar 28, 2019
For temporary workaround,
I'm doing it like this with promise:
Or with callback function:
Is this plausible at all?
boxcee commentedon Mar 28, 2019
@shian15810 Did you try? Does it work?
shian15810 commentedon Mar 28, 2019
Yeah, it works!
By plausible I mean does this really make sense?
magnusp commentedon Mar 28, 2019
Seems reasonable. I'm guessing that the proxyReq.socket should probably be aborted when an error occurs otherwise it could be left hanging until aborted by an timeout in either end.
shian15810 commentedon Mar 29, 2019
@magnusp Do you mean something like
proxyReq.socket.end()afterres.sendStatus(500)?Or should it be better off if I get rid of
res.sendStatus(500)and useproxyReq.socket.end('HTTP/1.1 500 Internal Server Error\r\n\r\n')instead?Edited 1:
Just check the source code in express, express's
res.sendStatuswill triggerres.sendwithin itself and in the end trigger node's httpres.end.Edited 2:
Just tested,
socket.resumeis not necessary when usingres.sendStatus.darewreck54 commentedon May 1, 2019
@shian15810 How did you set the header. When i try to set the header via proxyReq.setHeader('a','g'), i get an exception throwing
Cannot set header after they are sent to the client.
how did you get around that part? I followed the first promise example
Also not sure what the deal is but the res object has not sendStatus() method.....
Thanks!
shian15810 commentedon May 2, 2019
@darewreck54 I was having this issue too, and after looking into the source code, at least for me, I couldn't find any way to circumvent this situation, even when using
req.headers[customHeader].But having custom header is essential to the functioning of my application. So finally I decided to change the whole architecture to microservice-based. Sorry, couldn't be much of your help.
If you don't need custom header for your application to be functional, the hack I proposed earlier is still a feasible way to get around the original issue.
Using
req.headers[customHeader]won't give you that annoying error, but the header won't reach the upstream server anyway.darewreck54 commentedon May 2, 2019
I actually end up creating my own express middleware that execute the promise and when its successful it will trigger next(). I'll store the updated header in the request and send it to the proxy middleware
kavitalikesfajitas commentedon Jul 24, 2019
@chimurai did you create a solution to this? Also if not can you provide an example of the authValidator you mentioned?
1 remaining item
dpetrov commentedon Feb 4, 2020
@shian15810 I somehow can't get it quite to work:
which results to:
Is anything that I might be missing?
dpetrov commentedon Feb 4, 2020
So for the others that might be looking, I've ended up as well writing a middleware that stores the value into the req. For example:
and later on:
Probably not plausible at all, but it does work.
revolunet commentedon Apr 17, 2020
Thanks @dpetrov this works here too. you dont even have to use
onProxyReq, headers will get forwarded by the proxy later onkwiss commentedon May 1, 2020
no way @revolunet you are here too
Austio commentedon May 29, 2020
Are there any constraints in the library that would block a community PR for this @chimurai?
Was able to work around with a middleware that fires before my proxy middleware
smileatom commentedon Oct 2, 2020
There was a PR last year on http-proxy for this. And without async support in handlers even though it might be somewhat trivial to implement, I didnt really get a warm fuzzy when I looked at the internals.
Ill just use a library with keep alive and call it good.
maapteh commentedon Nov 11, 2020
@chimurai Steven bouw deze feature even erin man :)
we also want the async option, just before we stream back the results we want to alter header with async data.
i will checkout the mentioned node-http-proxy because it has selfHandleResponse option
shihshen commentedon Nov 19, 2020
I tried with selfHandleResponse option, and it seems working in this middleware as well.
maapteh commentedon Nov 19, 2020
@chimurai Steven, ik heb hem werkend :)
So i have this setup where i can still alter the response header AFTER i get the response from the proxied endpoint and do async stuff. In this sample i also included the fake server im mocking to (3001). Maybe nice for your recipe? Its setting response header on the response AFTER getting the response and do async stuff.
https://codesandbox.io/s/holy-resonance-yz552?file=/src/index.js
curl -I http://localhost:3000gives now:where mpth-2 is set async.
all i had to do was use
selfHandleResponse: true,andproxyRes.pipe(res);just by digging in the code :(thanks @powpowshen for telling it is possible, it made me look again :)
zhuibo66 commentedon Aug 14, 2021
your answer is good
sarumont commentedon Sep 2, 2021
Just a note to anyone else looking - the posted workaround which pauses the socket seems to not work with a Websocket
upgrade.Edit:
I did find a workaround here - handle the server's
upgradeand callproxy.upgradefrom your async handler:blastshielddown commentedon Nov 3, 2021
@dpetrov and @revolunet you are lifesavers. This worked for me 👍 Thank you
token exchange via middleware
Hosstell commentedon Jun 19, 2024
@pawelszczerbicki
delete http-proxy-middleware and use just http-proxy
my example:
dschweinbenz commentedon Aug 14, 2024
2024: Still no async/await support :/