1
+ #!/usr/local/bin/node
2
+
3
+ var httpProxy = require ( 'http-proxy' ) ,
4
+ http = require ( 'http' ) ,
5
+ util = require ( 'util' ) ,
6
+ colors = require ( 'colors' ) ;
7
+
8
+
9
+ exports . bodyMod = function ( ) {
10
+ console . log ( 'middleware has been started.' . green ) ;
11
+ return function ( req , res , next ) {
12
+ var proxy = next ,
13
+ total = '' ;
14
+
15
+ req . on ( 'data' , function ( data ) {
16
+ console . log ( 'ON DATA' )
17
+ total += data ;
18
+ } ) ;
19
+ req . on ( 'end' , function ( ) {
20
+ console . log ( 'ON END' )
21
+ console . log ( total ) ;
22
+ // This line, uncommented, hangs forever.
23
+ // proxy.proxyRequest(req, res, { port: 9000, host: 'localhost' });
24
+ // The following also hangs forever.
25
+ // next.proxyRequest(req, res, { port: 9000, host: 'localhost' });
26
+ } )
27
+ // The following fires just fine.
28
+ //proxy.proxyRequest(req, res, { port: 9000, host: 'localhost' });
29
+ console . log ( 'request proxied...' . blue ) ;
30
+ }
31
+ }
32
+
33
+ var proxyServer = httpProxy . createServer (
34
+ // Your middleware stack goes here.
35
+ exports . bodyMod ( )
36
+ ) . listen ( 8000 ) ;
37
+
38
+
39
+ var httpServer = http . createServer ( function ( req , res ) {
40
+ res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
41
+ res . write ( 'request successfully proxied to: ' + req . url + '\n' + JSON . stringify ( req . headers , true , 2 ) ) ;
42
+ res . end ( ) ;
43
+ } ) . listen ( 9000 ) ;
0 commit comments