当前位置: 首页 > 后端技术 > Node.js

如何优雅的给nodejs的http.get和http.request加上timeout?

时间:2023-04-03 16:13:35 Node.js

在网上搜了一堆settimeout/clearTimeout的使用教程。一开始我也是按照他妈的教程来的。结果报各种错误。最后仔细看了文档,发现req.setTimeout有这个方法。varreq=http.get('url',function(){})req.setTimeout(10000,function(){})varreq=http.request('url',function(){})req.setTimeout(10000,function(){})也比较好用,不会出现其他人为错误。