Skip to content

Commit 3768cce

Browse files
committed
Merge pull request #712 from zhudan/master
update modify request body eg
2 parents 9577a0f + d46e876 commit 3768cce

File tree

1 file changed

+50
-76
lines changed

1 file changed

+50
-76
lines changed

Diff for: examples/middleware/bodyDecoder-middleware.js

+50-76
Original file line numberDiff line numberDiff line change
@@ -29,91 +29,65 @@ var http = require('http'),
2929
request = require('request'),
3030
colors = require('colors'),
3131
util = require('util'),
32-
Store = require('../helpers/store'),
32+
bodyParser = require('body-parser'),
3333
httpProxy = require('../../lib/http-proxy'),
3434
proxy = httpProxy.createProxyServer({});
3535

36-
http.createServer(new Store().handler()).listen(7531, function () {
37-
util.puts('http '.blue + 'greetings '.green + 'server'.blue + ' started '.green.bold + 'on port '.blue + '7531'.yellow);
38-
//try these commands:
39-
// get index:
40-
// curl localhost:7531
41-
// []
42-
//
43-
// get a doc:
44-
// curl localhost:7531/foo
45-
// {"error":"not_found"}
46-
//
47-
// post an doc:
48-
// curl -X POST localhost:7531/foo -d '{"content": "hello", "type": "greeting"}'
49-
// {"ok":true}
50-
//
51-
// get index (now, not empty)
52-
// curl localhost:7531
53-
// ["/foo"]
54-
//
55-
// get doc
56-
// curl localhost:7531/foo
57-
// {"content": "hello", "type": "greeting"}
58-
59-
//
60-
// now, suppose we wanted to direct all objects where type == "greeting" to a different store
61-
// than where type == "insult"
62-
//
63-
// we can use connect connect-bodyDecoder and some custom logic to send insults to another Store.
64-
65-
//insult server:
6636

67-
http.createServer(new Store().handler()).listen(2600, function () {
68-
util.puts('http '.blue + 'insults '.red + 'server'.blue + ' started '.green.bold + 'on port '.blue + '2600'.yellow);
37+
//restreame
38+
var restreamer = function (){
39+
return function (req, res, next) { //restreame
40+
req.removeAllListeners('data')
41+
req.removeAllListeners('end')
42+
next()
43+
process.nextTick(function () {
44+
if(req.body) {
45+
req.emit('data', JSON.stringify(req.body))
46+
}
47+
req.emit('end')
48+
})
49+
}
50+
}
6951

70-
//greetings -> 7531, insults-> 2600
7152

72-
// now, start a proxy server.
53+
//
54+
// Basic Http Proxy Server
55+
//
56+
var app = connect()
57+
.use(bodyParser.json())//json
58+
.use(restreamer())//restreame
59+
.use(function(req, res){
60+
// modify body here,
61+
// eg: req.body = {a: 1}.
62+
console.log('proxy body:',req.body)
63+
proxy.web(req, res, {
64+
target: 'http://127.0.0.1:9013'
65+
})
66+
});
7367

74-
//don't worry about incoming contont type
75-
//bodyParser.parse[''] = JSON.parse
68+
http.createServer(app).listen(8013, function(){
69+
console.log('proxy linsten 8013');
70+
});
7671

77-
connect.createServer(
78-
//refactor the body parser and re-streamer into a separate package
79-
connect.bodyParser(),
80-
//body parser absorbs the data and end events before passing control to the next
81-
// middleware. if we want to proxy it, we'll need to re-emit these events after
82-
//passing control to the middleware.
83-
require('connect-restreamer')(),
84-
function (req, res) {
85-
//if your posting an obect which contains type: "insult"
86-
//it will get redirected to port 2600.
87-
//normal get requests will go to 7531 nad will not return insults.
88-
var port = (req.body && req.body.type === 'insult' ? 2600 : 7531)
89-
proxy.web(req, res, { target: { host: 'localhost', port: port }});
90-
}
91-
).listen(1337, function () {
92-
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '1337'.yellow);
93-
//bodyParser needs content-type set to application/json
94-
//if we use request, it will set automatically if we use the 'json:' field.
95-
function post (greeting, type) {
96-
request.post({
97-
url: 'http://localhost:1337/' + greeting,
98-
json: {content: greeting, type: type || "greeting"}
99-
})
100-
}
101-
post("hello")
102-
post("g'day")
103-
post("kiora")
104-
post("houdy")
105-
post("java", "insult")
10672

107-
//now, the insult should have been proxied to 2600
108-
109-
//curl localhost:2600
110-
//["/java"]
11173

112-
//but the greetings will be sent to 7531
74+
//
75+
// Target Http Server
76+
//
77+
var app1 = connect()
78+
.use(bodyParser.json())
79+
.use(function(req, res){
80+
console.log('app1:',req.body)
81+
res.end('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
82+
});
83+
http.createServer(app1).listen(9013, function(){
84+
//request to 8013 to proxy
85+
request.post({//
86+
url: 'http://127.0.0.1:8013',
87+
json: {content: 123, type: "greeting"}
88+
},function(err, res,data){
89+
console.log('return:' ,err, data)
90+
})
91+
});
11392

114-
//curl localhost:7531
115-
//["/hello","/g%27day","/kiora","/houdy"]
11693

117-
})
118-
})
119-
});

0 commit comments

Comments
 (0)