【问题标题】:On Node.js can I send a request and close it before the answer?在 Node.js 上,我可以在回答之前发送请求并关闭它吗?
【发布时间】:2013-08-21 23:08:55
【问题描述】:

我了解通常的HTTP请求生命周期,我想做的是:

向端点发出请求,当所有数据都发送完毕后,我想立即关闭连接,忽略任何可能的答案。

请注意,我不想只是忽略答案,我真的不想听一个,因为连接将被关闭 =)

【问题讨论】:

  • 请注意,根据端点及其行为方式,如果客户端在收到响应之前断开连接,则无法保证您的请求会完成。
  • 您想要的是简单地忽略而不是等待响应...请参阅下面的答案...您可以只从另一台服务器 .resume() 获得客户端的响应,这将使响应运行而不等待结果。

标签: node.js request httprequest


【解决方案1】:

只是不要连接客户端数据事件。

app.post('/something', function(req,res) {
   client.post(...., function(res2){
       //let the posted value and response finish
       //won't do anything with it
       res2.resume(); 

       //if you want to return after post, but before response data is read.
       //comment out the res.end below, and use this one.
       // res.end("done");
   });

   //if you are just wanting to fire and forget the post request...
   res.end("done");
});

【讨论】:

  • 注意:如果您想限制出站连接,您可能需要将客户端包装在通用池中...
猜你喜欢
  • 2013-11-04
  • 1970-01-01
  • 2010-09-19
  • 1970-01-01
  • 1970-01-01
  • 2015-08-09
  • 1970-01-01
  • 2023-02-20
  • 1970-01-01
相关资源
最近更新 更多