question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to access session inside onProxyReqWs

See original GitHub issue

Hello, 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:open
  • Created 4 years ago
  • Reactions:3
  • Comments:7

github_iconTop GitHub Comments

3reactions
CrazyPandarcommented, Nov 18, 2019

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:

const WsProxy = HPM(...);
DiskManager.use('/socket', function(req, res, next){
    console.log("entering first handler")
    next();
});
DiskManager.use('/socket', WsProxy);

If incoming request is websocket request, the “entering first handler” will not appear.

So weird!!! How can we solve this problem?

2reactions
mrtnbrodercommented, Sep 18, 2020

@MatthiasGrandl

My solution:

// configure express app however you like, then:
const server = app.listen(port)

const graphqlProxy = createProxyMiddleware({
  target: GRAPHQL_URI,
  changeOrigin: true,
  ws: true,
})

// Re-enable sessions on websockets. Middlewares are not passed to our websocket server and therefore our required `req.session` on the request is not available.
server.on("upgrade", (req, socket, head) => {
  // return a promise to make sure the session is added to the request
  // before anything else will be called.
  return new Promise((resolve) => {
    session(req, {} as any, () => {
      // req.session is now available and will be passed alongside the request
      graphqlProxy.upgrade?.(req, socket, head)
      resolve()
    })
  })
})

Hope this helps!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found