【问题标题】:node.js http-proxy how to set timeout and a handler on requestnode.js http-proxy 如何根据请求设置超时和处理程序
【发布时间】:2013-06-18 19:42:19
【问题描述】:

我在 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();
  });

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

【问题讨论】:

    标签: node.js node-http-proxy


    【解决方案1】:

    你可能想试试这个:

    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();
    });   
    

    【讨论】:

      猜你喜欢
      • 2013-12-21
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 2019-08-16
      相关资源
      最近更新 更多