Skip to content
Permalink
Browse files

Restream body before proxying (#1027)

Support for bodyparser.json and bodyparser.urlencoded.

Fixes #955 #843 #791
  • Loading branch information...
matibek authored and jcrugzz committed Aug 11, 2016
1 parent 183b5bb commit d0e000e1f91a969f1711eb3be7d1acb16c4538df
Showing with 22 additions and 17 deletions.
  1. +22 −17 examples/middleware/bodyDecoder-middleware.js
@@ -34,28 +34,25 @@ var http = require('http'),
proxy = httpProxy.createProxyServer({});


//restreame
var restreamer = function (){
return function (req, res, next) { //restreame
req.removeAllListeners('data')
req.removeAllListeners('end')
next()
process.nextTick(function () {
if(req.body) {
req.emit('data', JSON.stringify(req.body))
}
req.emit('end')
})
//restream parsed body before proxying
proxy.on('proxyReq', function(proxyReq, req, res, options) {
if(req.body) {
let bodyData = JSON.stringify(req.body);
// incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
proxyReq.setHeader('Content-Type','application/json');
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
// stream the content
proxyReq.write(bodyData);
}
}
});


//
// Basic Http Proxy Server
//
var app = connect()
.use(bodyParser.json())//json
.use(restreamer())//restreame
.use(bodyParser.json())//json parser
.use(bodyParser.urlencoded())//urlencoded parser
.use(function(req, res){
// modify body here,
// eg: req.body = {a: 1}.
@@ -84,9 +81,17 @@ http.createServer(app1).listen(9013, function(){
//request to 8013 to proxy
request.post({//
url: 'http://127.0.0.1:8013',
json: {content: 123, type: "greeting"}
json: {content: 123, type: "greeting from json request"}
},function(err, res,data){
console.log('return for json request:' ,err, data)
})

// application/x-www-form-urlencoded request
request.post({//
url: 'http://127.0.0.1:8013',
form: {content: 123, type: "greeting from urlencoded request"}
},function(err, res,data){
console.log('return:' ,err, data)
console.log('return for urlencoded request:' ,err, data)
})
});

0 comments on commit d0e000e

Please sign in to comment.
You can’t perform that action at this time.