Sri*_*jit 5 node.js node-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
})
Run Code Online (Sandbox Code Playgroud)
这设置超时(由于原因未知,它在17秒内超时)但回调从未执行.它只显示标准浏览器消息
The connection was reset
The connection to the server was reset while the page was loading.
Run Code Online (Sandbox Code Playgroud)
提前致谢
更新:
我试过了
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();
});
Run Code Online (Sandbox Code Playgroud)
这次调用该方法,但它抱怨已经发送了响应,因此无法设置标头
您可能想尝试这个:
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();
});
Run Code Online (Sandbox Code Playgroud)