Unable to access session inside onProxyReqWs
See original GitHub issueHello, I have issue: I want to use onProxyReqWs for authentication. I need to take my session data and send it to websocket server as a header. But my connect middlewares are not getting called before proxy in case of websocket connection. Here is example:
app.use((req, res, next) => {
req.test = 123
})
app.use(proxy({
changeOrigin: true,
ws: true,
target: 'http://localhost:7045',
onProxyReq(proxyReq, req, res) {
// Everything is correct, req.test are set here
console.log('onProxyReq triggered: ', req.test)
proxyReq.setHeader('x-secret', req.test);
},
onProxyReqWs(proxyReq, req, sock, options, head) {
// Whoops, req.test are undefined!
console.log('onProxyReqWs triggered: ', req.test)
}
))
I don’t understand why and most importantly, HOW it bypasses middleware chain, since proxy is a middleware itself. Can you please explain this magic and advice me how to get session data from previous middlewares?
The problem is that I use it inside nuxt, which only supports low-level middlewares (e.g. only use()), so I cannot attach anything to connect app.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:7
Top Results From Across the Web
http-proxy-middleware create new session for per request
I build a proxy with http-proxy-middleware and it generates new session for every request, thus I can ...
Read more >Nodejs Server to Server API integration Token Issue
Which I believe Im doing by creating a proxy route in my nodejs application using express app.use('/proxy', verifyAuth,
Read more >Using the Backstage Proxy from Within a Plugin
Setting up the backstage proxy. Let's say your plugin's API is hosted at https://api.myawesomeservice.com/v1, and you want to be able to access it...
Read more >Mastering Angular proxy configuration | by JM Robles - Medium
WTF can't access to my backend! Angular CLI provides a solution to bypass these problems: reverse proxy server.
Read more >http-proxy-middleware
Note: In multiple path matching, you cannot use string paths and ... onProxyReq: function, subscribe to http-proxy's proxyReq event.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have met the same problem. It seems if proxy for websocket, the HPM will overwrite previous express handler. let’s look at the code below:
If incoming request is websocket request, the “entering first handler” will not appear.
So weird!!! How can we solve this problem?
@MatthiasGrandl
My solution:
Hope this helps!