@@ -45,14 +45,14 @@ exports.NodeProxy.prototype = {
45
45
this . events = [ ] ;
46
46
47
47
this . onData = function ( ) {
48
- self . events . push ( [ " data" ] . concat ( self . toArray ( arguments ) ) ) ;
48
+ self . events . push ( [ ' data' ] . concat ( self . toArray ( arguments ) ) ) ;
49
49
} ;
50
50
this . onEnd = function ( ) {
51
- self . events . push ( [ " end" ] . concat ( self . toArray ( arguments ) ) ) ;
51
+ self . events . push ( [ ' end' ] . concat ( self . toArray ( arguments ) ) ) ;
52
52
} ;
53
53
54
- req . addListener ( " data" , this . onData ) ;
55
- req . addListener ( " end" , this . onEnd ) ;
54
+ req . addListener ( ' data' , this . onData ) ;
55
+ req . addListener ( ' end' , this . onEnd ) ;
56
56
} ,
57
57
58
58
proxyRequest : function ( server , port , req , res ) {
@@ -67,45 +67,45 @@ exports.NodeProxy.prototype = {
67
67
var reverse_proxy = c . request ( req . method , req . url , req . headers ) ;
68
68
69
69
// Add a listener for the connection timeout event
70
- reverse_proxy . connection . addListener ( 'error' , function ( err ) {
70
+ reverse_proxy . connection . addListener ( 'error' , function ( err ) {
71
71
res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
72
72
73
73
if ( req . method !== 'HEAD' ) {
74
- res . write ( 'Not a HEAD request' ) ;
74
+ res . write ( 'An error has occurred: ' + sys . puts ( JSON . stringify ( err ) ) ) ;
75
75
}
76
76
77
77
res . end ( ) ;
78
78
} ) ;
79
79
80
80
// Add a listener for the reverse_proxy response event
81
- reverse_proxy . addListener ( " response" , function ( response ) {
81
+ reverse_proxy . addListener ( ' response' , function ( response ) {
82
82
// Set the response headers of the client response
83
83
res . writeHead ( response . statusCode , response . headers ) ;
84
84
85
85
// Add event handler for the proxied response in chunks
86
- response . addListener ( " data" , function ( chunk ) {
86
+ response . addListener ( ' data' , function ( chunk ) {
87
87
if ( req . method !== 'HEAD' ) {
88
88
res . write ( chunk , 'binary' ) ;
89
- this . body += chunk ;
89
+ self . body += chunk ;
90
90
}
91
91
} ) ;
92
92
93
93
// Add event listener for end of proxied response
94
- response . addListener ( "end" , function ( ) {
94
+ response . addListener ( 'end' , function ( ) {
95
+ // Remark: Emit the end event for testability
96
+ self . emitter . emit ( 'end' , null , self . body ) ;
97
+
95
98
res . end ( ) ;
96
99
} ) ;
97
100
} ) ;
98
101
99
102
// Chunk the client request body as chunks from the proxied request come in
100
- req . addListener ( " data" , function ( chunk ) {
103
+ req . addListener ( ' data' , function ( chunk ) {
101
104
reverse_proxy . write ( chunk , 'binary' ) ;
102
105
} )
103
106
104
107
// At the end of the client request, we are going to stop the proxied request
105
- req . addListener ( "end" , function ( ) {
106
- // Remark: Emit the end event for testability
107
- self . emitter . emit ( 'something' , self . body ) ;
108
-
108
+ req . addListener ( 'end' , function ( ) {
109
109
reverse_proxy . end ( ) ;
110
110
} ) ;
111
111
0 commit comments