我在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秒内超时)但回调从未执行。它只显示标准浏览器消息
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();
});
这次调用该方法,但是它抱怨响应已经发送,因此无法设置标题
答案 0 :(得分:2)
你可能想试试这个:
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();
});