Created
April 30, 2014 17:39
-
-
Save pulkitsinghal/5ab774376338ae37b9a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -XPOST http://localhost:5000/test/post/proxy -H "content-type: application/json;charset=UTF-8" -d '{"dum":"dum"}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "proxy", | |
"version": "0.1.0", | |
"description": "proxy", | |
"main": "server.js", | |
"repository": "", | |
"keywords": [ | |
"proxy" | |
], | |
"author": "Pulkit Singhal", | |
"license": "Apache", | |
"readmeFilename": "README.md", | |
"dependencies": { | |
"http-proxy": "1.1.2", | |
"express": "4.1.1", | |
"body-parser": "1.0.2", | |
"connect-restreamer": "1.0.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http') | |
, httpProxy = require('http-proxy') | |
, express = require('express') | |
, bodyParser = require('body-parser'); | |
var proxy = httpProxy.createProxyServer({}); | |
var app = express(); | |
app.use(bodyParser()); | |
app.use(require('connect-restreamer')()); | |
app.all('/test/post/proxy', function (req, res) { | |
console.log('/test/post/proxy \n matched \t' + req.url); | |
req.url = '/dummy'; | |
console.log(' re-routing \t' + req.url); | |
var substituteBodyWithThis = { | |
"a": { | |
"b": { | |
"c": [] | |
} | |
} | |
}; | |
console.log('before change: ' + req.body); | |
req.body = JSON.stringify(substituteBodyWithThis); | |
console.log('after change: ' + req.body); | |
proxy.web(req, res, { target: 'http://localhost:9000' }); | |
}); | |
var port = Number(process.env.PORT || 5000); | |
app.listen(port, function() { | |
console.log("Listening on " + port); | |
}); | |
http.createServer(function (req, res) { | |
if (req.method == 'POST') { | |
console.log('1'); | |
var body = ''; | |
req.on('data', function (data) { | |
console.log('2'); | |
body += data; | |
}); | |
req.on('end', function () { | |
console.log('3'); | |
console.log('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2) + '\n' + body); | |
res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2) + '\n' + body); | |
res.end(); | |
}); | |
req.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
console.log('4'); | |
} | |
console.log('5'); | |
}).listen(9000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment