Skip to content
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

Making onProxyRes function asynchronous friendly #1689

Open
rainb3rry opened this issue Nov 18, 2024 · 1 comment
Open

Making onProxyRes function asynchronous friendly #1689

rainb3rry opened this issue Nov 18, 2024 · 1 comment

Comments

@rainb3rry
Copy link

rainb3rry commented Nov 18, 2024

I would like to be able to set response after a specific time passed as my code below, but it's not possible.

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');

const app = express();

const asyncFunc = () => {
  return new Promise((resolve) => {
    setTimeout(() => resolve("Hello World!"), 3000)
  })
}


const onProxyRes = async function  (proxyRes, req, res) {


  const result = await asyncFunc()

  res.write(result);
  res.end();

};


const exampleProxy = createProxyMiddleware({
  target: 'https://example.com',
  changeOrigin: true,
  on: { proxyRes: onProxyRes }
});
app.use('/', exampleProxy);



app.listen(3003)

But if I remove const result = await asyncFunc() part, I can set response anything I want inside of res.write();.

@ravin00
Copy link

ravin00 commented Nov 28, 2024

if you remove the const result = await asyncFunc() the request will not pass to the backend and the function will no longer wait for the async() to finish. instead of that always use await() when using async().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants