Skip to content

Commit 7cd407e

Browse files
authored
docs(createProxyMiddleware): add createProxyMiddleware jsdoc (#1212)
1 parent a9ea1e4 commit 7cd407e

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

src/factory.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,82 @@ import type * as http from 'node:http';
33
import { HttpProxyMiddleware } from './http-proxy-middleware.js';
44
import type { NextFunction, Options, RequestHandler } from './types.js';
55

6+
/**
7+
* Create proxy middleware for Express-like servers. ([list of servers with examples](https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/servers.md))
8+
*
9+
* @example Basic proxy to a single target.
10+
* ```ts
11+
* import { createProxyMiddleware } from 'http-proxy-middleware';
12+
*
13+
* const proxy = createProxyMiddleware({
14+
* target: 'http://www.example.org',
15+
* changeOrigin: true,
16+
* });
17+
* ```
18+
*
19+
* @example Proxy only matching paths and rewrite the forwarded path.
20+
* ```ts
21+
* import { createProxyMiddleware } from 'http-proxy-middleware';
22+
*
23+
* const proxy = createProxyMiddleware({
24+
* target: 'http://localhost:3000',
25+
* pathFilter: '/api',
26+
* pathRewrite: {
27+
* '^/api/': '/',
28+
* },
29+
* });
30+
* ```
31+
*
32+
* @example Native path rewrite by mounting at a route (alternative to `pathRewrite`).
33+
* ```ts
34+
* import express from 'express';
35+
* import { createProxyMiddleware } from 'http-proxy-middleware';
36+
*
37+
* const app = express();
38+
* app.use(
39+
* '/users',
40+
* createProxyMiddleware({
41+
* target: 'http://jsonplaceholder.typicode.com/users',
42+
* changeOrigin: true,
43+
* }),
44+
* );
45+
* ```
46+
*
47+
* @example Use framework-specific request/response types (Express).
48+
* ```ts
49+
* import type { Request, Response } from 'express';
50+
* import { createProxyMiddleware } from 'http-proxy-middleware';
51+
*
52+
* const proxy = createProxyMiddleware<Request, Response>({
53+
* target: 'http://www.example.org/api',
54+
* changeOrigin: true,
55+
* });
56+
* ```
57+
*
58+
* @example Intercept and modify a proxied response body.
59+
* ```ts
60+
* import { createProxyMiddleware, responseInterceptor } from 'http-proxy-middleware';
61+
*
62+
* const proxy = createProxyMiddleware({
63+
* target: 'http://www.example.org',
64+
* selfHandleResponse: true,
65+
* on: {
66+
* proxyRes: responseInterceptor(async (responseBuffer) => {
67+
* const response = responseBuffer.toString('utf8');
68+
* return response.replace('Hello', 'Goodbye');
69+
* }),
70+
* },
71+
* });
72+
* ```
73+
*
74+
* @see https://github.com/chimurai/http-proxy-middleware/
75+
* @see https://github.com/chimurai/http-proxy-middleware/#basic-usage
76+
* @see https://github.com/chimurai/http-proxy-middleware/#intercept-and-manipulate-responses
77+
* @see https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/servers.md
78+
* @see https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathFilter.md
79+
* @see https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathRewrite.md
80+
* @see https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/response-interceptor.md
81+
*/
682
export function createProxyMiddleware<
783
TReq extends http.IncomingMessage = http.IncomingMessage,
884
TRes extends http.ServerResponse = http.ServerResponse,

0 commit comments

Comments
 (0)