@@ -29,91 +29,65 @@ var http = require('http'),
29
29
request = require ( 'request' ) ,
30
30
colors = require ( 'colors' ) ,
31
31
util = require ( 'util' ) ,
32
- Store = require ( '../helpers/store ' ) ,
32
+ bodyParser = require ( 'body-parser ' ) ,
33
33
httpProxy = require ( '../../lib/http-proxy' ) ,
34
34
proxy = httpProxy . createProxyServer ( { } ) ;
35
35
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:
66
36
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
+ }
69
51
70
- //greetings -> 7531, insults-> 2600
71
52
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
+ } ) ;
73
67
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
+ } ) ;
76
71
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" )
106
72
107
- //now, the insult should have been proxied to 2600
108
-
109
- //curl localhost:2600
110
- //["/java"]
111
73
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
+ } ) ;
113
92
114
- //curl localhost:7531
115
- //["/hello","/g%27day","/kiora","/houdy"]
116
93
117
- } )
118
- } )
119
- } ) ;
0 commit comments