Skip to content

feat: response interceptor #515

Closed
@chimurai

Description

@chimurai

Add responseInterceptor handler to easily intercept and manipulate responses. Responses compressed with brotli, gzip and deflate are decompressed automatically.

install beta: npm install http-proxy-middleware@1.2.0-beta.2 or yarn add http-proxy-middleware@1.2.0-beta.2

TL;DR

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

const proxy = createProxyMiddleware({
  /**
   * IMPORTANT: avoid res.end being called automatically
   **/
  selfHandleResponse: true, // res.end() will be called internally by responseInterceptor()

  /**
   * Intercept response and replace 'Hello' with 'Goodbye'
   **/
  onProxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
    const response = responseBuffer.toString('utf-8'); // convert buffer to string
    return response.replace('Hello', 'Goodbye'); // manipulate response and return the result
  }),
});

documentation: https://github.com/chimurai/http-proxy-middleware/tree/response-interceptor#intercept-and-manipulate-responses

recipes: https://github.com/chimurai/http-proxy-middleware/blob/response-interceptor/recipes/response-interceptor.md#readme

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions