Skip to content

docs: fix proxy events documentation #887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2023
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
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