1
- var httpProxy = require ( '../index' ) ;
1
+ var httpProxy = require ( '../lib/http-proxy' ) ,
2
+ http = require ( 'http' ) ;
2
3
/*
3
4
* Create your proxy server
4
5
*/
5
- var proxyServer = httpProxy . createProxyServer ( { target :'http://localhost:30404' , ws :true } ) ;
6
+ var proxy = httpProxy . createProxyServer ( { target :'http://localhost:30404' , ws :true } ) ;
6
7
7
- // Register an error handler for web requests
8
- proxyServer . ee . on ( "http-proxy:outgoing:web:error" , function ( err , req , res ) {
9
- res . writeHead ( 502 ) ;
10
- res . end ( "There was an error proxying your request" ) ;
11
- } ) ;
8
+ var proxyServer = http . createServer ( requestHandler ) ;
12
9
13
- // Register an error handler for web-socket requests
14
- proxyServer . ee . on ( "http-proxy:outgoing:ws:error" , function ( err , req , socket , head ) {
15
- socket . close ( ) ;
16
- } ) ;
17
-
18
- // You may also use a wild card
19
- proxyServer . ee . on ( "*:*:*:error" , function ( err , req ) {
20
- console . log ( "The error event '" + this . event + "' happened errno: " + err . errno ) ;
21
- } ) ;
10
+ function requestHandler ( req , res ) {
11
+ // Pass a callback to the web proxy method
12
+ // and catch the error there.
13
+ proxy . web ( req , res , function ( err ) {
14
+ // Now you can get the err
15
+ // and handle it by your self
16
+ // if (err) throw err;
17
+ res . writeHead ( 502 ) ;
18
+ res . end ( "There was an error proxying your request" ) ;
19
+ } ) ;
22
20
21
+ // In a websocket request case
22
+ req . on ( 'upgrade' , function ( req , socket , head ) {
23
+ proxy . ws ( req , socket , head , function ( err ) {
24
+ // Now you can get the err
25
+ // and handle it by your self
26
+ // if (err) throw err;
27
+ socket . close ( ) ;
28
+ } )
29
+ } )
30
+ }
23
31
24
32
console . log ( "Proxy server is listening on port 8000" ) ;
25
- proxyServer . listen ( 8000 ) ;
26
-
33
+ proxyServer . listen ( 8000 )
0 commit comments