Skip to content

Commit 8e42628

Browse files
committed
docs(proxy events): fix new syntax (#753)
1 parent 77be17a commit 8e42628

File tree

3 files changed

+104
-82
lines changed

3 files changed

+104
-82
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,9 @@ const proxy = createProxyMiddleware({
510510
/**
511511
* Fix bodyParser
512512
**/
513-
onProxyReq: fixRequestBody,
513+
on: {
514+
proxyReq: fixRequestBody,
515+
},
514516
});
515517
```
516518

@@ -538,10 +540,12 @@ const proxy = createProxyMiddleware({
538540
/**
539541
* Intercept response and replace 'Hello' with 'Goodbye'
540542
**/
541-
onProxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
542-
const response = responseBuffer.toString('utf8'); // convert buffer to string
543-
return response.replace('Hello', 'Goodbye'); // manipulate response and return the result
544-
}),
543+
on: {
544+
proxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
545+
const response = responseBuffer.toString('utf8'); // convert buffer to string
546+
return response.replace('Hello', 'Goodbye'); // manipulate response and return the result
547+
}),
548+
},
545549
});
546550
```
547551

recipes/async-response.md

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@ const myProxy = createProxyMiddleware({
77
target: 'http://www.example.com/api',
88
changeOrigin: true,
99
selfHandleResponse: true,
10-
onProxyReq: (proxyReq, req, res) => {
11-
// before
12-
proxyReq.setHeader('mpth-1', 'da');
13-
},
14-
onProxyRes: async (proxyRes, req, res) => {
15-
const da = await new Promise((resolve, reject) => {
16-
setTimeout(() => {
17-
resolve({ wei: 'wei' });
18-
}, 200);
19-
});
10+
on: {
11+
proxyReq: (proxyReq, req, res) => {
12+
// before
13+
proxyReq.setHeader('mpth-1', 'da');
14+
},
15+
}
16+
on: {
17+
proxyRes: async (proxyRes, req, res) => {
18+
const da = await new Promise((resolve, reject) => {
19+
setTimeout(() => {
20+
resolve({ wei: 'wei' });
21+
}, 200);
22+
});
2023

21-
// add your dynamic header
22-
res.setHeader('mpth-2', da.wei);
24+
// add your dynamic header
25+
res.setHeader('mpth-2', da.wei);
2326

24-
// now pipe the response
25-
proxyRes.pipe(res);
26-
},
27+
// now pipe the response
28+
proxyRes.pipe(res);
29+
},
30+
}
2731
});
2832

2933
app.use('/api', myProxy);
@@ -48,25 +52,29 @@ const myProxy = createProxyMiddleware({
4852
target: 'http://www.example.com/api',
4953
changeOrigin: true,
5054
selfHandleResponse: true,
51-
onProxyReq: (proxyReq, req, res) => {
52-
// before
53-
// get something async from entry middleware before the proxy kicks in
54-
console.log('proxyReq:', req.locals.da);
55+
on: {
56+
proxyReq: (proxyReq, req, res) => {
57+
// before
58+
// get something async from entry middleware before the proxy kicks in
59+
console.log('proxyReq:', req.locals.da);
5560

56-
proxyReq.setHeader('mpth-1', req.locals.da);
57-
},
58-
onProxyRes: async (proxyRes, req, res) => {
59-
const da = await new Promise((resolve, reject) => {
60-
setTimeout(() => {
61-
resolve({ wei: 'wei' });
62-
}, 200);
63-
});
61+
proxyReq.setHeader('mpth-1', req.locals.da);
62+
},
63+
}
64+
on: {
65+
proxyRes: async (proxyRes, req, res) => {
66+
const da = await new Promise((resolve, reject) => {
67+
setTimeout(() => {
68+
resolve({ wei: 'wei' });
69+
}, 200);
70+
});
6471

65-
// end:
66-
res.setHeader('mpth-2', da.wei);
72+
// end:
73+
res.setHeader('mpth-2', da.wei);
6774

68-
proxyRes.pipe(res);
69-
},
75+
proxyRes.pipe(res);
76+
},
77+
}
7078
});
7179

7280
app.use('/api', entryMiddleware, myProxy);

recipes/response-interceptor.md

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ const proxy = createProxyMiddleware({
2121
/**
2222
* Intercept response and replace 'Hello' with 'Teapot' with 418 http response status code
2323
**/
24-
onProxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
25-
res.statusCode = 418; // set different response status code
26-
27-
const response = responseBuffer.toString('utf8');
28-
return response.replace('Hello', 'Teapot');
29-
}),
24+
on: {
25+
proxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
26+
res.statusCode = 418; // set different response status code
27+
28+
const response = responseBuffer.toString('utf8');
29+
return response.replace('Hello', 'Teapot');
30+
}),
31+
},
3032
});
3133
```
3234

@@ -39,17 +41,19 @@ const proxy = createProxyMiddleware({
3941

4042
selfHandleResponse: true, // res.end() will be called internally by responseInterceptor()
4143

42-
onProxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
43-
// log original request and proxied request info
44-
const exchange = `[DEBUG] ${req.method} ${req.path} -> ${proxyRes.req.protocol}//${proxyRes.req.host}${proxyRes.req.path} [${proxyRes.statusCode}]`;
45-
console.log(exchange); // [DEBUG] GET / -> http://www.example.com [200]
44+
on: {
45+
proxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
46+
// log original request and proxied request info
47+
const exchange = `[DEBUG] ${req.method} ${req.path} -> ${proxyRes.req.protocol}//${proxyRes.req.host}${proxyRes.req.path} [${proxyRes.statusCode}]`;
48+
console.log(exchange); // [DEBUG] GET / -> http://www.example.com [200]
4649

47-
// log complete response
48-
const response = responseBuffer.toString('utf8');
49-
console.log(response); // log response body
50+
// log complete response
51+
const response = responseBuffer.toString('utf8');
52+
console.log(response); // log response body
5053

51-
return responseBuffer;
52-
}),
54+
return responseBuffer;
55+
}),
56+
},
5357
});
5458
```
5559

@@ -62,21 +66,23 @@ const proxy = createProxyMiddleware({
6266

6367
selfHandleResponse: true, // res.end() will be called internally by responseInterceptor()
6468

65-
onProxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
66-
// detect json responses
67-
if (proxyRes.headers['content-type'] === 'application/json') {
68-
let data = JSON.parse(responseBuffer.toString('utf8'));
69+
on: {
70+
proxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
71+
// detect json responses
72+
if (proxyRes.headers['content-type'] === 'application/json') {
73+
let data = JSON.parse(responseBuffer.toString('utf8'));
6974

70-
// manipulate JSON data here
71-
data = Object.assign({}, data, { extra: 'foo bar' });
75+
// manipulate JSON data here
76+
data = Object.assign({}, data, { extra: 'foo bar' });
7277

73-
// return manipulated JSON
74-
return JSON.stringify(data);
75-
}
78+
// return manipulated JSON
79+
return JSON.stringify(data);
80+
}
7681

77-
// return other content-types as-is
78-
return responseBuffer;
79-
}),
82+
// return other content-types as-is
83+
return responseBuffer;
84+
}),
85+
},
8086
});
8187
```
8288

@@ -107,23 +113,25 @@ const proxy = createProxyMiddleware({
107113

108114
selfHandleResponse: true, // res.end() will be called internally by responseInterceptor()
109115

110-
onProxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
111-
const imageTypes = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif'];
112-
113-
// detect image responses
114-
if (imageTypes.includes(proxyRes.headers['content-type'])) {
115-
try {
116-
const image = await Jimp.read(responseBuffer);
117-
image.flip(true, false).sepia().pixelate(5);
118-
return image.getBufferAsync(Jimp.AUTO);
119-
} catch (err) {
120-
console.log('image processing error: ', err);
121-
return responseBuffer;
116+
on: {
117+
proxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
118+
const imageTypes = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif'];
119+
120+
// detect image responses
121+
if (imageTypes.includes(proxyRes.headers['content-type'])) {
122+
try {
123+
const image = await Jimp.read(responseBuffer);
124+
image.flip(true, false).sepia().pixelate(5);
125+
return image.getBufferAsync(Jimp.AUTO);
126+
} catch (err) {
127+
console.log('image processing error: ', err);
128+
return responseBuffer;
129+
}
122130
}
123-
}
124131

125-
return responseBuffer; // return other content-types as-is
126-
}),
132+
return responseBuffer; // return other content-types as-is
133+
}),
134+
},
127135
});
128136

129137
// http://localhost:3000/wikipedia/en/7/7d/Lenna\_%28test_image%29.png
@@ -146,9 +154,11 @@ const proxy = createProxyMiddleware({
146154
/**
147155
* Intercept response and remove the
148156
**/
149-
onProxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
150-
res.removeHeader('content-security-policy'); // Remove the Content Security Policy header
151-
res.setHeader('HPM-Header', 'Intercepted by HPM'); // Set a new header and value
152-
}),
157+
on: {
158+
proxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
159+
res.removeHeader('content-security-policy'); // Remove the Content Security Policy header
160+
res.setHeader('HPM-Header', 'Intercepted by HPM'); // Set a new header and value
161+
}),
162+
},
153163
});
154164
```

0 commit comments

Comments
 (0)