From 04c45a022ca880bdfc2be3e05359331ef3558f99 Mon Sep 17 00:00:00 2001 From: dan <464033315@qq.com> Date: Thu, 9 Oct 2014 14:30:11 +0800 Subject: [PATCH 1/3] update modify request body eg --- examples/middleware/bodyDecoder-middleware.js | 128 +++++++----------- 1 file changed, 52 insertions(+), 76 deletions(-) diff --git a/examples/middleware/bodyDecoder-middleware.js b/examples/middleware/bodyDecoder-middleware.js index 555c3d154..01511fc72 100644 --- a/examples/middleware/bodyDecoder-middleware.js +++ b/examples/middleware/bodyDecoder-middleware.js @@ -29,91 +29,67 @@ var http = require('http'), request = require('request'), colors = require('colors'), util = require('util'), - Store = require('../helpers/store'), + bodyParser = require('body-parser'), httpProxy = require('../../lib/http-proxy'), proxy = httpProxy.createProxyServer({}); -http.createServer(new Store().handler()).listen(7531, function () { - util.puts('http '.blue + 'greetings '.green + 'server'.blue + ' started '.green.bold + 'on port '.blue + '7531'.yellow); -//try these commands: -// get index: -// curl localhost:7531 -// [] -// -// get a doc: -// curl localhost:7531/foo -// {"error":"not_found"} -// -// post an doc: -// curl -X POST localhost:7531/foo -d '{"content": "hello", "type": "greeting"}' -// {"ok":true} -// -// get index (now, not empty) -// curl localhost:7531 -// ["/foo"] -// -// get doc -// curl localhost:7531/foo -// {"content": "hello", "type": "greeting"} - -// -// now, suppose we wanted to direct all objects where type == "greeting" to a different store -// than where type == "insult" -// -// we can use connect connect-bodyDecoder and some custom logic to send insults to another Store. - -//insult server: - http.createServer(new Store().handler()).listen(2600, function () { - util.puts('http '.blue + 'insults '.red + 'server'.blue + ' started '.green.bold + 'on port '.blue + '2600'.yellow); +//restreame +var restreamer = function (){ + return function (req, res, next) { //restreame + req.removeAllListeners('data') + req.removeAllListeners('end') + next() + process.nextTick(function () { + rawBody = new Buffer(req.body) + console.log('dsfgsdgsdgsdrtwergtsdgf', JSON.stringify(req.body).length) + if(req.body) { + req.emit('data', JSON.stringify(req.body)) + } + req.emit('end') + }) + } +} - //greetings -> 7531, insults-> 2600 - // now, start a proxy server. +// +// Basic Http Proxy Server +// +var app = connect() + .use(bodyParser.json())//json + .use(restreamer())//restreame + .use(function(req, res){ + // modify body here, + // eg: req.body = {a: 1}. + console.log('proxy body:',req.body) + proxy.web(req, res, { + target: 'http://127.0.0.1:9013' + }) + }); - //don't worry about incoming contont type - //bodyParser.parse[''] = JSON.parse +http.createServer(app).listen(8013, function(){ + console.log('proxy linsten 8013'); +}); - connect.createServer( - //refactor the body parser and re-streamer into a separate package - connect.bodyParser(), - //body parser absorbs the data and end events before passing control to the next - // middleware. if we want to proxy it, we'll need to re-emit these events after - //passing control to the middleware. - require('connect-restreamer')(), - function (req, res) { - //if your posting an obect which contains type: "insult" - //it will get redirected to port 2600. - //normal get requests will go to 7531 nad will not return insults. - var port = (req.body && req.body.type === 'insult' ? 2600 : 7531) - proxy.web(req, res, { target: { host: 'localhost', port: port }}); - } - ).listen(1337, function () { - util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '1337'.yellow); - //bodyParser needs content-type set to application/json - //if we use request, it will set automatically if we use the 'json:' field. - function post (greeting, type) { - request.post({ - url: 'http://localhost:1337/' + greeting, - json: {content: greeting, type: type || "greeting"} - }) - } - post("hello") - post("g'day") - post("kiora") - post("houdy") - post("java", "insult") - //now, the insult should have been proxied to 2600 - - //curl localhost:2600 - //["/java"] - //but the greetings will be sent to 7531 +// +// Target Http Server +// +var app1 = connect() + .use(bodyParser.json()) + .use(function(req, res){ + console.log('app1:',req.body) + res.end('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2)); + }); +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"} + },function(err, res,data){ + console.log('return:' ,err, data) + }) +}); - //curl localhost:7531 - //["/hello","/g%27day","/kiora","/houdy"] - }) - }) -}); \ No newline at end of file From 0378b03e5301afb7d0249bf75ae06afacf09bbb8 Mon Sep 17 00:00:00 2001 From: dan <464033315@qq.com> Date: Thu, 9 Oct 2014 14:32:52 +0800 Subject: [PATCH 2/3] update modify request body eg --- examples/middleware/bodyDecoder-middleware.js | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/middleware/bodyDecoder-middleware.js b/examples/middleware/bodyDecoder-middleware.js index 01511fc72..80e3dfb87 100644 --- a/examples/middleware/bodyDecoder-middleware.js +++ b/examples/middleware/bodyDecoder-middleware.js @@ -42,7 +42,6 @@ var restreamer = function (){ next() process.nextTick(function () { rawBody = new Buffer(req.body) - console.log('dsfgsdgsdgsdrtwergtsdgf', JSON.stringify(req.body).length) if(req.body) { req.emit('data', JSON.stringify(req.body)) } From d46e876e273f7b11ca092d519ec5d0e82839638d Mon Sep 17 00:00:00 2001 From: dan <464033315@qq.com> Date: Thu, 9 Oct 2014 14:33:04 +0800 Subject: [PATCH 3/3] update modify request body eg --- examples/middleware/bodyDecoder-middleware.js | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/middleware/bodyDecoder-middleware.js b/examples/middleware/bodyDecoder-middleware.js index 80e3dfb87..00c97f909 100644 --- a/examples/middleware/bodyDecoder-middleware.js +++ b/examples/middleware/bodyDecoder-middleware.js @@ -41,7 +41,6 @@ var restreamer = function (){ req.removeAllListeners('end') next() process.nextTick(function () { - rawBody = new Buffer(req.body) if(req.body) { req.emit('data', JSON.stringify(req.body)) }