Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/handlers/fix-request-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ export type BodyParserLikeRequest = http.IncomingMessage & { body?: any };

/**
* Fix proxied body if bodyParser is involved.
*
* @example
* ```ts
* createProxyMiddleware({
* target: 'http://example.com',
* on: {
* proxyReq: fixRequestBody,
* }
* });
* ```
*
* Alternative solution without using `fixRequestBody()`: put `http-proxy-middleware` before `bodyParser` in the middleware stack.
*
* @see {@link https://github.com/chimurai/http-proxy-middleware/issues/40 Github issue #40 - POST request body is not proxied}
*/
export function fixRequestBody<TReq extends BodyParserLikeRequest = BodyParserLikeRequest>(
proxyReq: http.ClientRequest,
Expand Down
16 changes: 16 additions & 0 deletions src/handlers/response-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ type Interceptor<TReq = http.IncomingMessage, TRes = http.ServerResponse> = (
* Give developer the opportunity to modify intercepted Buffer and http.ServerResponse
*
* NOTE: must set options.selfHandleResponse=true (prevent automatic call of res.end())
*
* @example
*
* ```ts
* createProxyMiddleware({
* target: 'http://example.com',
* selfHandleResponse: true, // MUST set selfHandleResponse=true
* on: {
* proxyRes: responseInterceptor(async (buffer, proxyRes, req, res) => {
* // modify intercepted buffer and return modified buffer
* const modifiedBuffer = Buffer.from(buffer.toString().replace(/Example/g, 'Demo'), 'utf8');
* return modifiedBuffer;
* }),
* }
* });
* ```
*/
export function responseInterceptor<
TReq extends http.IncomingMessage = http.IncomingMessage,
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export type Filter<TReq extends http.IncomingMessage = http.IncomingMessage> =
| string[]
| ((pathname: string, req: TReq) => boolean);

/**
* @see {@link https://github.com/chimurai/http-proxy-middleware/tree/master#defineplugin-helper `definePlugin()`} to define a http-proxy-middleware plugin.
*/
export interface Plugin<
TReq extends http.IncomingMessage = http.IncomingMessage,
TRes extends http.ServerResponse = http.ServerResponse,
Expand Down
Loading