Skip to content

Fix headings & syntax highlighting #313

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
Nov 25, 2018
Merged
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
130 changes: 66 additions & 64 deletions recipes/modify-post.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
##Modify Post Parameters:
## Modify Post Parameters:
The code example below illustrates how to modify POST body data prior to forwarding to the proxy target.
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.

Expand All @@ -8,67 +8,69 @@ Since this only modifys the request body stream the original POST body parameter

## Example:

'use strict';
var express = require('express');
var ProxyMiddleware = require('http-proxy-middleware');
var router = express.Router();
var proxy_filter = function (path, req) {
return path.match('^/docs') && ( req.method === 'GET' || req.method === 'POST' );
};
var proxy_options = {
target: 'http://localhost:8080',
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();
}
```js
'use strict';

var express = require('express');
var ProxyMiddleware = require('http-proxy-middleware');
var router = express.Router();

var proxy_filter = function (path, req) {
return path.match('^/docs') && ( req.method === 'GET' || req.method === 'POST' );
};

var proxy_options = {
target: 'http://localhost:8080',
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();
}
};

// Proxy configuration
var proxy = ProxyMiddleware( proxy_filter, proxy_options );

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Node.js Express Proxy Test' });
});

router.all('/docs', proxy );

module.exports = router;
}
};

// Proxy configuration
var proxy = ProxyMiddleware( proxy_filter, proxy_options );

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Node.js Express Proxy Test' });
});

router.all('/docs', proxy );

module.exports = router;
```