-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[examples] update the error-handling example using the new error hand…
…le way
- Loading branch information
Showing
2 changed files
with
26 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,33 @@ | ||
var httpProxy = require('../index'); | ||
var httpProxy = require('../lib/http-proxy'), | ||
http = require('http'); | ||
/* | ||
* Create your proxy server | ||
*/ | ||
var proxyServer = httpProxy.createProxyServer({target:'http://localhost:30404', ws:true}); | ||
var proxy = httpProxy.createProxyServer({target:'http://localhost:30404', ws:true}); | ||
|
||
// Register an error handler for web requests | ||
proxyServer.ee.on("http-proxy:outgoing:web:error", function(err, req, res){ | ||
res.writeHead(502); | ||
res.end("There was an error proxying your request"); | ||
}); | ||
var proxyServer = http.createServer(requestHandler); | ||
|
||
// Register an error handler for web-socket requests | ||
proxyServer.ee.on("http-proxy:outgoing:ws:error", function(err, req, socket, head){ | ||
socket.close(); | ||
}); | ||
|
||
// You may also use a wild card | ||
proxyServer.ee.on("*:*:*:error", function(err, req){ | ||
console.log("The error event '" + this.event + "' happened errno: " + err.errno); | ||
}); | ||
function requestHandler(req, res) { | ||
// Pass a callback to the web proxy method | ||
// and catch the error there. | ||
proxy.web(req, res, function (err) { | ||
// Now you can get the err | ||
// and handle it by your self | ||
// if (err) throw err; | ||
res.writeHead(502); | ||
res.end("There was an error proxying your request"); | ||
}); | ||
|
||
// In a websocket request case | ||
req.on('upgrade', function (req, socket, head) { | ||
proxy.ws(req, socket, head, function (err) { | ||
// Now you can get the err | ||
// and handle it by your self | ||
// if (err) throw err; | ||
socket.close(); | ||
}) | ||
}) | ||
} | ||
|
||
console.log("Proxy server is listening on port 8000"); | ||
proxyServer.listen(8000); | ||
|
||
proxyServer.listen(8000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters