Skip to content

Commit f979128

Browse files
authored
docs: fix proxy events documentation (#887)
1 parent 6e7eb1f commit f979128

File tree

2 files changed

+41
-43
lines changed

2 files changed

+41
-43
lines changed

recipes/async-response.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ const myProxy = createProxyMiddleware({
1212
// before
1313
proxyReq.setHeader('mpth-1', 'da');
1414
},
15-
}
16-
on: {
1715
proxyRes: async (proxyRes, req, res) => {
1816
const da = await new Promise((resolve, reject) => {
1917
setTimeout(() => {
@@ -27,7 +25,7 @@ const myProxy = createProxyMiddleware({
2725
// now pipe the response
2826
proxyRes.pipe(res);
2927
},
30-
}
28+
},
3129
});
3230

3331
app.use('/api', myProxy);
@@ -60,8 +58,6 @@ const myProxy = createProxyMiddleware({
6058

6159
proxyReq.setHeader('mpth-1', req.locals.da);
6260
},
63-
}
64-
on: {
6561
proxyRes: async (proxyRes, req, res) => {
6662
const da = await new Promise((resolve, reject) => {
6763
setTimeout(() => {
@@ -74,7 +70,7 @@ const myProxy = createProxyMiddleware({
7470

7571
proxyRes.pipe(res);
7672
},
77-
}
73+
},
7874
});
7975

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

recipes/modify-post.md

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,45 @@ const proxy_options = {
2525
pathRewrite: {
2626
'^/docs': '/java/rep/server1', // Host path & target path conversion
2727
},
28-
onError(err, req, res) {
29-
res.writeHead(500, {
30-
'Content-Type': 'text/plain',
31-
});
32-
res.end('Something went wrong. And we are reporting a custom error message.' + err);
33-
},
34-
onProxyReq(proxyReq, req, res) {
35-
if (req.method == 'POST' && req.body) {
36-
// Add req.body logic here if needed....
37-
38-
// ....
39-
40-
// Remove body-parser body object from the request
41-
if (req.body) delete req.body;
42-
43-
// Make any needed POST parameter changes
44-
let body = new Object();
45-
46-
body.filename = 'reports/statistics/summary_2016.pdf';
47-
body.routeId = 's003b012d002';
48-
body.authId = 'bac02c1d-258a-4177-9da6-862580154960';
49-
50-
// URI encode JSON object
51-
body = Object.keys(body)
52-
.map(function (key) {
53-
return encodeURIComponent(key) + '=' + encodeURIComponent(body[key]);
54-
})
55-
.join('&');
56-
57-
// Update header
58-
proxyReq.setHeader('content-type', 'application/x-www-form-urlencoded');
59-
proxyReq.setHeader('content-length', body.length);
60-
61-
// Write out body changes to the proxyReq stream
62-
proxyReq.write(body);
63-
proxyReq.end();
64-
}
28+
on: {
29+
error(err, req, res) {
30+
res.writeHead(500, {
31+
'Content-Type': 'text/plain',
32+
});
33+
res.end('Something went wrong. And we are reporting a custom error message.' + err);
34+
},
35+
proxyReq(proxyReq, req, res) {
36+
if (req.method == 'POST' && req.body) {
37+
// Add req.body logic here if needed....
38+
39+
// ....
40+
41+
// Remove body-parser body object from the request
42+
if (req.body) delete req.body;
43+
44+
// Make any needed POST parameter changes
45+
let body = new Object();
46+
47+
body.filename = 'reports/statistics/summary_2016.pdf';
48+
body.routeId = 's003b012d002';
49+
body.authId = 'bac02c1d-258a-4177-9da6-862580154960';
50+
51+
// URI encode JSON object
52+
body = Object.keys(body)
53+
.map(function (key) {
54+
return encodeURIComponent(key) + '=' + encodeURIComponent(body[key]);
55+
})
56+
.join('&');
57+
58+
// Update header
59+
proxyReq.setHeader('content-type', 'application/x-www-form-urlencoded');
60+
proxyReq.setHeader('content-length', body.length);
61+
62+
// Write out body changes to the proxyReq stream
63+
proxyReq.write(body);
64+
proxyReq.end();
65+
}
66+
},
6567
},
6668
};
6769

0 commit comments

Comments
 (0)