Node.js 请求在ExpressJS上提前结束
我用Node.js 请求在ExpressJS上提前结束,node.js,express,curl,Node.js,Express,Curl,我用curl提出请求 router.route('...') .get(function(req, res, next) { // This job() function doing some *long* async task and end of the task calling 3th param as callback job(param1, param2, function(response) { // printing response to console
curl
提出请求
router.route('...')
.get(function(req, res, next) {
// This job() function doing some *long* async task and end of the task calling 3th param as callback
job(param1, param2, function(response) {
// printing response to console perfectly
console.log("callback fired", response);
res.send("response");
});
});
cURL在等待响应几分钟,然后我得到了空的回复错误。cURL在nodejs打印回调触发消息之前发出此错误
如果我使用浏览器或邮递员发出此请求,则会出现相同的错误
我确信
job()
异步函数中没有res.send()
,res.end()
函数。我卡住了,我如何跟踪并发现错误?有两种可能的超时可能会影响您。如果看不到实际的网络跟踪(看看请求结束时会发生什么),我无法判断是哪个超时导致了您的问题。但是,您可以同时解决这两个问题,它应该能够解决您的问题
首先,curl
有一个超时值。尚不清楚其默认设置是什么,但您可以使用以下选项设置任何值:
$ curl ... -m 300
curl: (52) Empty reply from server
其中值以秒为单位
其次,nodejshttp服务器有一个超时,如果没有响应打开的请求,它将关闭套接字(这可以防止死套接字随着时间的推移而累积)。您可以查看server.setTimeout()
的文档。http服务器对象的默认值是2分钟(我不知道Express是否会改变这一点)
总体思路是:
curl --max-time 900
其中,
server
对象将是您的http服务器对象(而不是Expressapp
对象)。您猜我的问题是http服务器的默认超时设置。所以我把这个值从2分钟增加到5分钟,我的问题就解决了。非常感谢。
server.setTimeout(10 * 60 * 1000); // set response timeout to 10 minutes