added error handling when HTTP proxy doesn't connect

This commit is contained in:
tigerbot 2017-05-09 16:23:30 -06:00
parent ab011d1829
commit bcba0abddc
1 changed files with 12 additions and 0 deletions

View File

@ -64,6 +64,18 @@ module.exports.create = function (deps, conf) {
proxyReq.setHeader('X-Forwarded-For', opts.remoteAddress || conn.remoteAddress); proxyReq.setHeader('X-Forwarded-For', opts.remoteAddress || conn.remoteAddress);
}); });
proxy.on('error', function (err, req, res) {
console.log(err);
res.writeHead(502);
if (err.code === 'ECONNREFUSED') {
res.end('The connection was refused. Most likely the service being connected to '
+ 'has stopped running or the configuration is wrong.');
}
else {
res.end('Bad Gateway: ' + err.code);
}
});
return function (req, res, next) { return function (req, res, next) {
var hostname = req.headers.host.split(':')[0]; var hostname = req.headers.host.split(':')[0];
var relevant = mod.domains.some(function (pattern) { var relevant = mod.domains.some(function (pattern) {