1
- ##Modify Post Parameters:
1
+ ## Modify Post Parameters:
2
2
The code example below illustrates how to modify POST body data prior to forwarding to the proxy target.
3
3
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.
4
4
@@ -8,67 +8,69 @@ Since this only modifys the request body stream the original POST body parameter
8
8
9
9
## Example:
10
10
11
- 'use strict';
12
-
13
- var express = require('express');
14
- var ProxyMiddleware = require('http-proxy-middleware ');
15
- var router = express.Router( );
16
-
17
- var proxy_filter = function (path, req) {
18
- return path.match('^/docs') && ( req.method === 'GET' || req.method === 'POST' );
19
- } ;
20
-
21
- var proxy_options = {
22
- target: 'http://localhost:8080',
23
- pathRewrite: {
24
- '^/docs' : '/java/rep/server1' // Host path & target path conversion
25
- },
26
- onError(err, req, res) {
27
- res.writeHead(500, {
28
- 'Content-Type': 'text/plain'
29
- });
30
- res.end('Something went wrong. And we are reporting a custom error message.' + err );
31
- },
32
- onProxyReq(proxyReq, req, res) {
33
- if ( req.method == "POST" && req.body ) {
34
- // Add req.body logic here if needed....
35
-
36
- // ....
37
-
38
- // Remove body-parser body object from the request
39
- if ( req. body ) delete req.body;
40
-
41
- // Make any needed POST parameter changes
42
- let body = new Object();
43
-
44
- body.filename = 'reports/statistics/summary_2016.pdf';
45
- body.routeid = 's003b012d002 ';
46
- body.authid = 'bac02c1d-258a-4177-9da6-862580154960 ';
47
-
48
- // URI encode JSON object
49
- body = Object.keys( body ).map(function( key ) {
50
- return encodeURIComponent( key ) + '=' + encodeURIComponent( body[ key ])
51
- }).join('&');
52
-
53
- // Update header
54
- proxyReq.setHeader( 'content-type', 'application/x-www-form-urlencoded' );
55
- proxyReq.setHeader( 'content-length ', body.length );
56
-
57
- // Write out body changes to the proxyReq stream
58
- proxyReq.write( body );
59
- proxyReq.end( );
60
- }
11
+ ``` js
12
+ ' use strict ' ;
13
+
14
+ var express = require (' express ' );
15
+ var ProxyMiddleware = require ( ' http-proxy-middleware ' );
16
+ var router = express . Router ();
17
+
18
+ var proxy_filter = function ( path , req ) {
19
+ return path . match ( ' ^/docs ' ) && ( req . method === ' GET ' || req . method === ' POST ' ) ;
20
+ };
21
+
22
+ var proxy_options = {
23
+ target : ' http://localhost:8080 ' ,
24
+ pathRewrite : {
25
+ ' ^/docs ' : ' /java/rep/server1 ' // Host path & target path conversion
26
+ },
27
+ onError ( err , req , res ) {
28
+ res . writeHead ( 500 , {
29
+ ' Content-Type ' : ' text/plain '
30
+ } );
31
+ res . end ( ' Something went wrong. And we are reporting a custom error message. ' + err);
32
+ },
33
+ onProxyReq ( proxyReq , req , res ) {
34
+ if ( req . method == " POST " && req .body ) {
35
+ // Add req.body logic here if needed....
36
+
37
+ // ....
38
+
39
+ // Remove body-parser body object from the request
40
+ if ( req . body ) delete req . body ;
41
+
42
+ // Make any needed POST parameter changes
43
+ let body = new Object ();
44
+
45
+ body .filename = ' reports/statistics/summary_2016.pdf ' ;
46
+ body .routeid = ' s003b012d002 ' ;
47
+ body . authid = ' bac02c1d-258a-4177-9da6-862580154960 ' ;
48
+
49
+ // URI encode JSON object
50
+ body = Object . keys ( body ). map ( function ( key ) {
51
+ return encodeURIComponent ( key ) + ' = ' + encodeURIComponent ( body[ key ])
52
+ }). join ( ' & ' );
53
+
54
+ // Update header
55
+ proxyReq .setHeader ( ' content-type ' , ' application/x-www-form-urlencoded ' );
56
+ proxyReq . setHeader ( ' content-length ' , body . length );
57
+
58
+ // Write out body changes to the proxyReq stream
59
+ proxyReq .write ( body );
60
+ proxyReq . end ();
61
61
}
62
- };
63
-
64
- // Proxy configuration
65
- var proxy = ProxyMiddleware( proxy_filter, proxy_options );
66
-
67
- /* GET home page. */
68
- router.get('/', function(req, res, next) {
69
- res.render('index', { title: 'Node.js Express Proxy Test' });
70
- });
71
-
72
- router.all('/docs', proxy );
73
-
74
- module.exports = router;
62
+ }
63
+ };
64
+
65
+ // Proxy configuration
66
+ var proxy = ProxyMiddleware ( proxy_filter, proxy_options );
67
+
68
+ /* GET home page. */
69
+ router .get (' /' , function (req , res , next ) {
70
+ res .render (' index' , { title: ' Node.js Express Proxy Test' });
71
+ });
72
+
73
+ router .all (' /docs' , proxy );
74
+
75
+ module .exports = router;
76
+ ```
0 commit comments