|
1 | | -## Modify Post Parameters: |
| 1 | +## Modify POST body |
2 | 2 |
|
3 | | -The code example below illustrates how to modify POST body data prior to forwarding to the proxy target. |
4 | | -Key to this example is the _"OnProxyReq"_ event handler that creates a new POST body that can be manipulated to format the POST data as required. For example: inject new POST parameters that should only be visible server side. |
| 3 | +Use `body-parser` to populate `req.body`, mutate that object in `on.proxyReq`, then call `fixRequestBody` so the updated body is written to the outgoing proxy request. |
5 | 4 |
|
6 | | -This example uses the _"body-parser"_ module in the main app to create a req.body object with the decoded POST parameters. Side note - the code below will allow _"http-proxy-middleware"_ to work with _"body-parser"_. |
7 | | - |
8 | | -Since this only modifies the request body stream the original POST body parameters remain in tact, so any POST data changes will not be sent back in the response to the client. |
9 | | - |
10 | | -## Example: |
| 5 | +### Minimal example |
11 | 6 |
|
12 | 7 | ```js |
13 | | -'use strict'; |
14 | | - |
15 | 8 | import express from 'express'; |
16 | | -import { createProxyMiddleware } from 'http-proxy-middleware'; |
17 | | - |
18 | | -const router = express.Router(); |
19 | | - |
20 | | -const proxy_options = { |
21 | | - target: 'http://localhost:8080', |
22 | | - pathRewrite: { |
23 | | - '^/docs': '/java/rep/server1', // Host path & target path conversion |
24 | | - }, |
25 | | - pathFilter: function (path, req) { |
26 | | - return path.match('^/docs') && (req.method === 'GET' || req.method === 'POST'); |
27 | | - }, |
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); |
| 9 | +import bodyParser from 'body-parser'; |
| 10 | +import { createProxyMiddleware, fixRequestBody } from 'http-proxy-middleware'; |
| 11 | + |
| 12 | +const app = express(); |
| 13 | + |
| 14 | +app.use(bodyParser.json()); |
| 15 | + |
| 16 | +app.use( |
| 17 | + '/search', |
| 18 | + createProxyMiddleware({ |
| 19 | + target: 'http://localhost:4000', |
| 20 | + changeOrigin: true, |
| 21 | + on: { |
| 22 | + proxyReq(proxyReq, req) { |
| 23 | + if (req.method !== 'POST' || !req.body) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + // 1) modify the parsed body |
| 28 | + req.body.limit = 25; |
| 29 | + req.body.filters = ['public']; |
| 30 | + |
| 31 | + // 2) optional server-only header |
| 32 | + proxyReq.setHeader('x-api-key', process.env.SEARCH_API_KEY ?? ''); |
| 33 | + |
| 34 | + // 3) write modified body to the proxied request |
| 35 | + fixRequestBody(proxyReq, req); |
| 36 | + }, |
34 | 37 | }, |
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 | | - }, |
67 | | - }, |
68 | | -}; |
69 | | - |
70 | | -// Proxy configuration |
71 | | -const proxy = createProxyMiddleware(proxy_options); |
72 | | - |
73 | | -/* GET home page. */ |
74 | | -router.get('/', function (req, res, next) { |
75 | | - res.render('index', { title: 'Node.js Express Proxy Test' }); |
76 | | -}); |
| 38 | + }), |
| 39 | +); |
| 40 | +``` |
77 | 41 |
|
78 | | -router.all('/docs', proxy); |
| 42 | +### Essential points |
79 | 43 |
|
80 | | -module.exports = router; |
81 | | -``` |
| 44 | +- If `body-parser` runs before the proxy, the original request stream has already been consumed. |
| 45 | +- Updating `req.body` alone is not enough; call `fixRequestBody(proxyReq, req)` to forward the modified payload. |
| 46 | +- Keep mutations in `proxyReq` focused on request shaping (fields/headers) and avoid unrelated app logic in this handler. |
0 commit comments