2013-08-21 139 views
0

我瞭解通常的HTTP請求生命週期,我想要做的是:關於Node.js,我可以發送請求並在答案之前關閉它嗎?

向所有數據發送完成時向端點發出請求,我想立即關閉連接,忽略任何可能的答案。

請注意,我不想只是忽略了答案,我想從字面上不聽爲一體,因爲連接將被關閉=)

+0

請注意,根據端點及其表現如何,如果客戶端在接收響應之前斷開連接,則不能保證您的請求會完成。 – Joe

+0

你想要的只是簡單地忽略,而不是等待響應......見下面的答案......你可以讓客戶端的響應來自其他服務器.resume(),它會讓響應運行而不等待結果。 – Tracker1

回答

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"); 
}); 
+0

注意:如果你想限制出站連接,你可能想把你的客戶端包裝到一個通用池中。 – Tracker1

相關問題