Skip to content

Commit a1b25a1

Browse files
committed
[examples] update the error-handling example using the new error handle way
1 parent 920f1e7 commit a1b25a1

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

Diff for: examples/error-handling.js

+25-18
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
1-
var httpProxy = require('../index');
1+
var httpProxy = require('../lib/http-proxy'),
2+
http = require('http');
23
/*
34
* Create your proxy server
45
*/
5-
var proxyServer = httpProxy.createProxyServer({target:'http://localhost:30404', ws:true});
6+
var proxy = httpProxy.createProxyServer({target:'http://localhost:30404', ws:true});
67

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);
129

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+
});
2220

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+
}
2331

2432
console.log("Proxy server is listening on port 8000");
25-
proxyServer.listen(8000);
26-
33+
proxyServer.listen(8000)

Diff for: examples/stand-alone.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var http = require('http'),
2-
httpProxy = require('http-proxy');
2+
httpProxy = require('../lib/http-proxy');
33
//
44
// Create your proxy server
55
//

0 commit comments

Comments
 (0)