Skip to content

Commit

Permalink
docs: fix proxy events documentation (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai authored Mar 3, 2023
1 parent 6e7eb1f commit f979128
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
8 changes: 2 additions & 6 deletions recipes/async-response.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const myProxy = createProxyMiddleware({
// before
proxyReq.setHeader('mpth-1', 'da');
},
}
on: {
proxyRes: async (proxyRes, req, res) => {
const da = await new Promise((resolve, reject) => {
setTimeout(() => {
Expand All @@ -27,7 +25,7 @@ const myProxy = createProxyMiddleware({
// now pipe the response
proxyRes.pipe(res);
},
}
},
});

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

proxyReq.setHeader('mpth-1', req.locals.da);
},
}
on: {
proxyRes: async (proxyRes, req, res) => {
const da = await new Promise((resolve, reject) => {
setTimeout(() => {
Expand All @@ -74,7 +70,7 @@ const myProxy = createProxyMiddleware({

proxyRes.pipe(res);
},
}
},
});

app.use('/api', entryMiddleware, myProxy);
Expand Down
76 changes: 39 additions & 37 deletions recipes/modify-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,45 @@ const proxy_options = {
pathRewrite: {
'^/docs': '/java/rep/server1', // Host path & target path conversion
},
onError(err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain',
});
res.end('Something went wrong. And we are reporting a custom error message.' + err);
},
onProxyReq(proxyReq, req, res) {
if (req.method == 'POST' && req.body) {
// Add req.body logic here if needed....

// ....

// Remove body-parser body object from the request
if (req.body) delete req.body;

// Make any needed POST parameter changes
let body = new Object();

body.filename = 'reports/statistics/summary_2016.pdf';
body.routeId = 's003b012d002';
body.authId = 'bac02c1d-258a-4177-9da6-862580154960';

// URI encode JSON object
body = Object.keys(body)
.map(function (key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(body[key]);
})
.join('&');

// Update header
proxyReq.setHeader('content-type', 'application/x-www-form-urlencoded');
proxyReq.setHeader('content-length', body.length);

// Write out body changes to the proxyReq stream
proxyReq.write(body);
proxyReq.end();
}
on: {
error(err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain',
});
res.end('Something went wrong. And we are reporting a custom error message.' + err);
},
proxyReq(proxyReq, req, res) {
if (req.method == 'POST' && req.body) {
// Add req.body logic here if needed....

// ....

// Remove body-parser body object from the request
if (req.body) delete req.body;

// Make any needed POST parameter changes
let body = new Object();

body.filename = 'reports/statistics/summary_2016.pdf';
body.routeId = 's003b012d002';
body.authId = 'bac02c1d-258a-4177-9da6-862580154960';

// URI encode JSON object
body = Object.keys(body)
.map(function (key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(body[key]);
})
.join('&');

// Update header
proxyReq.setHeader('content-type', 'application/x-www-form-urlencoded');
proxyReq.setHeader('content-length', body.length);

// Write out body changes to the proxyReq stream
proxyReq.write(body);
proxyReq.end();
}
},
},
};

Expand Down

0 comments on commit f979128

Please sign in to comment.