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.

Service async function on onProxyReq

See original GitHub issue

Having proxy configured with onProxyReq like:

proxy(gatewayUrl, {
  target: destinationUrl,
  pathRewrite: { [`^${gatewayUrl}`]: '' },
  changeOrigin: true,
  logProvider: () => logger,
  ...options,
  onProxyReq: async (proxyReq, req) => await addAuthHeader(proxyReq, req, redis),
});

Middleware is finishing request before promise is resolved. It is possible to service promise in proxy functions ?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:66
  • Comments:28 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
SreeBScommented, 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.

10reactions
revolunetcommented, Apr 17, 2020

Thanks @dpetrov this works here too. you dont even have to use onProxyReq, headers will get forwarded by the proxy later on

server.use((req, res, next) => {
  if (req.path === "/graphql" && req.method === "POST") {
    getSomethingAsync(req.session.id, (err, data) => {
      if (data) {
        req.headers["Authorization"] = `Bearer ${data.user.token}`
      }
      next()
    })
  } else {
    next()
  }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Service async function on onProxyReq - - Bountysource
Service async function on onProxyReq ... Having proxy configured with onProxyReq like: proxy(gatewayUrl, { target: destinationUrl, pathRewrite: { [`^${gatewayUrl} ...
Read more >
Invoking an asynchronous method inside a middleware in ...
I'm trying to create a proxy with node-http-proxy in Node.js that checks whether a request is authorized in a mongodb. Basically, I created...
Read more >
http-proxy-middleware - npm
The one-liner node.js proxy middleware for connect, express and browser-sync. Latest version: 2.0.6, last published: 8 months ago.
Read more >
http-proxy-middleware: Documentation | Openbase
pathFilter (string, []string, glob, []glob, function) ... function onProxyReq(proxyReq, req, res) { // add custom header to request proxyReq.
Read more >
An introduction to API Gateway in NestJS with Microservices ...
async function bootstrap() { ... with Bearer JWT then you could have a service in the API Gateway that confirms that the JWT...
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