mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[examples] update the error-handling example using the new error handle way
This commit is contained in:
parent
920f1e7707
commit
a1b25a123b
@ -1,26 +1,33 @@
|
|||||||
var httpProxy = require('../index');
|
var httpProxy = require('../lib/http-proxy'),
|
||||||
|
http = require('http');
|
||||||
/*
|
/*
|
||||||
* Create your proxy server
|
* 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
|
var proxyServer = http.createServer(requestHandler);
|
||||||
proxyServer.ee.on("http-proxy:outgoing:web:error", function(err, req, res){
|
|
||||||
|
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.writeHead(502);
|
||||||
res.end("There was an error proxying your request");
|
res.end("There was an error proxying your request");
|
||||||
});
|
});
|
||||||
|
|
||||||
// Register an error handler for web-socket requests
|
// In a websocket request case
|
||||||
proxyServer.ee.on("http-proxy:outgoing:ws:error", function(err, req, socket, head){
|
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();
|
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
console.log("Proxy server is listening on port 8000");
|
console.log("Proxy server is listening on port 8000");
|
||||||
proxyServer.listen(8000);
|
proxyServer.listen(8000)
|
||||||
|
|
||||||
@ -1,5 +1,5 @@
|
|||||||
var http = require('http'),
|
var http = require('http'),
|
||||||
httpProxy = require('http-proxy');
|
httpProxy = require('../lib/http-proxy');
|
||||||
//
|
//
|
||||||
// Create your proxy server
|
// Create your proxy server
|
||||||
//
|
//
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user