在请求用户应用程序之前发生错误

我使用node-http代理模块来运行具有正常工作的反向代理的应用程序,在某些情况下,用户希望立即运行应用程序(应用程序尚未启动),并且可能需要大约3- 15秒,直到应用程序启动并运行。 在这种情况下,用户将从代理中得到错误

proxy.web(req, res, { target: 'http://' + hostname + ':' + Port console.log("App------------->Proxy App" ); }); proxy.on('proxyReq', function(proxyReq, req, res, options) { console.log("App------------->Proxy Request" ); }); proxy.on('error', function (err, req, res) { console.log("App------------->Proxy Error" ); res.end('Something went wrong'); }); // Listen for the `proxyRes` event on `proxy`. proxy.on('proxyRes', function (proxyRes, req, res) { console.log("App------------->Proxy Response" ) var respState = res.statusCode }); 

如果出现错误 ,日志中的堆栈就像

  1. 代理应用
  2. 代理请求
  3. 代理错误

在这种情况下,用户将在浏览器中运行应用程序url,并首先得到错误,几秒钟后刷新浏览器,应用程序将运行OK。 任何build议如何解决这个问题?

我想构build一些API来检查proxyRes状态 (比如每1秒调用一次,看看响应是否为200,而不是像以前那样发送错误,“超时检查”,如果10秒后没有响应可能发送错误,但不知道如何做到这一点,如果它的好办法…任何想法?或者也许通过webSoket,但不知道如何做到这一点…

这是使用https://github.com/nodejitsu/node-http-proxy的开源的Im

尝试在选项中设置proxyTimeout。 此标志没有logging,但在代码中使用:

https://github.com/nodejitsu/node-http-proxy/blob/master/lib/http-proxy/passes/web-incoming.js#L119

 var proxy = httpProxy.createProxyServer({proxyTimeout:3000}) // timeout 3 seconds