node.js http-proxy如何根据请求设置超时和处理程序

我在node.js上使用http-proxy。 我有一个代理请求到另一台服务器。 我的要求是代理请求应该在10秒内超时。 另外,当超时发生时,我应该能够显示一个自定义消息给用户

我有下面的代码

var proxy = new httpProxy.RoutingProxy(); req.on('error', function (err,req,res){ res.send("An error occured"); }); proxy.proxyRequest(req, res, { host: 'localhost', port: port, headers:req.headers, timeout:10000 }) 

这设置超时(由于未知的原因,它在17秒内超时),但callback从未执行。 它只是显示标准的浏览器消息

 The connection was reset The connection to the server was reset while the page was loading. 

提前致谢

更新

我试过了

 proxy.on('proxyError', function (err,preq,pres) { pres.writeHead(500, { 'Content-Type': 'text/plain' }); pres.write("An error happened at server. Please contact your administrator."); pres.end(); }); 

这一次该方法被调用,但它抱怨响应已经发送,所以不能设置标题

你可能想试试这个:

 proxy.on('error', function(err, preq, pres){ pres.writeHead(500, { 'Content-Type': 'text/plain' }); pres.write("An error happened at server. Please contact your administrator."); pres.end(); });