How to detect when target server is down in http-proxy?

≡放荡痞女 提交于 2019-12-12 23:54:05

问题


I'm trying to create an internet facing server for a internal node express server that I am running in a windows machine. For this purpose, I have a virtual unix box with ports opened and I am using http-proxy to make a proxy server which redirects request to my internal server.

I want to show the users a custom html page if the internal server is down. I have following code (relevant part) running in the virtual unix box:

var proxy = httpProxy.createProxyServer({target:'http://'+addr.to[2]+':'+addr.to[3], xfwd:true,       ws:true});

proxy.listen(addr.from[3], addr.from[2]);

winston.info('--Proxy server started listening on: '+addr.from[2]+':'+addr.from[3]);

proxy.on('error', function (err, req, res) {

  console.log(err);

  try{
  winston.error('Middleware server down, Delivering 404 page');
  res.writeHead(500, {
    'Content-Type': 'text/html'
   });

    fs.readFile(__dirname+'/404.html', function (err, html) {
        if (err) {
            throw err;
        }

        res.write(html);
        res.end();
    });


  }
  catch(er)
  {
    winston.error('Error:'+er);
  }

});

The above code works fine if I run it in windows machines. For some reason, it does not work when I run in the unix box. Can anyone help me understand why this is happening? It would be great if you could provide alternate means to detect when target server is down in http-proxy.

Let me know if you need any additional information.

来源:https://stackoverflow.com/questions/26584845/how-to-detect-when-target-server-is-down-in-http-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!