Skip to content

Making onProxyRes function asynchronous friendly #1689

@rainb3rry

Description

@rainb3rry

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();.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions