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

Add support for custom onProxyReq and onProxyRes #50

Closed
amaury1093 opened this issue Dec 20, 2021 · 4 comments
Closed

Add support for custom onProxyReq and onProxyRes #50

amaury1093 opened this issue Dec 20, 2021 · 4 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@amaury1093
Copy link

I think it would be useful to add onProxyReq and onProxyRes options in the constructor function:

httpProxyMiddleware(req, res, {
      // You can use the `http-proxy` option
      target: 'https://www.example.com',
      // In addition, you can use the `pathRewrite` option provided by `next-http-proxy-middleware`
      pathRewrite: [{
        patternStr: '^/api/new',
        replaceStr: '/v2'
      }, {
        patternStr: '^/api',
        replaceStr: ''
      }],
+     onProxyReq: (proxyReq, req, res) => {...},
+     onProxyRes: (proxyRes, req, res) => {...},
    })

This will allow more fine-grained request and response rewriting.

@stegano stegano added the good first issue Good for newcomers label Dec 21, 2021
@stegano
Copy link
Owner

stegano commented Dec 21, 2021

Hi @AmauryM
Please see #31
Thanks :)

@amaury1093
Copy link
Author

#31 seems to solve onProxyReq, thanks.

How about onProxyRes?

If I do:

async function handler(req: NextApiRequest, res: NextApiResponse): Promise<void> {
	return httpProxyMiddleware(req, res, {
		target: TARGET_HOST,
	})
	.then(() => {
		res.setHeader(...);
	})
}

then I get Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

@stegano stegano added the enhancement New feature or request label Jan 18, 2022
@stegano stegano self-assigned this Jan 22, 2022
stegano added a commit that referenced this issue Jan 22, 2022
* Added function to directly access `http-proxy` instance
   - Improved to be able to directly bind event to `http-proxy` instance.

related: #50 #31
@stegano
Copy link
Owner

stegano commented Jan 22, 2022

Hi @AmauryM
From v1.2.0 version, you can access the http-proxy instance through an option. Please refer to the document. 😀
https://github.com/stegano/next-http-proxy-middleware#onproxyinit-option

@amaury1093
Copy link
Author

Sounds good, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants