Router proxy means no proxyError :(
Created by: temsa
I have multiple host possible for my target, as I can have several stateless servers behind (for the moment, just 2).
I'd like, if ever the proxy request encounters an error (like ECONNREFUSED), to be able to redirect to another host. I couldn't make it work because I would need both:
- Router based proxy
- connect middlewares
- 'proxyError' event to be thrown by my server.proxy.on('proxyError')
The latter is never emitted in this configuration.
(I'd also need for hang up errors a target connection timeout to be sent so I can dispatch the request to another server in this case).
Try this script (adapted from 2 examples), proxyError is never emitted :
var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('http-proxy');
var server = httpProxy.createServer({
router: {
'localhost': 'localhost:9000'
}
})
server.proxy.on('proxyError', function (err, req, res) {
console.log('error'.red);
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong. And we are reporting a custom error message.');
});
server.listen(8001);
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with proxy table'.magenta.underline);