nodeJS防止res.download超时 [英] nodeJS prevent timeout on res.download

查看:323
本文介绍了nodeJS防止res.download超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我的nodeJS服务器进行POST调用,该服务器在mongo数据库中搜索一些数据,并返回包含所请求数据的CSV文件。问题是数据搜索和处理超过2分钟的nodeJS默认超时。



在使用y的不同情况下:

  res.writeHeader( 200,'application / json'); 
res.write(开始获取....);

通过发送一些使请求保持活动状态并防止客户端超时res.write(''); 不时。



现在我使用 res.download()下载生成的csv文件,因此不仅发送JSON作为响应。



试图使用这种解决方案:

  res.writeHeader (200,'application / json'); 
res.write(开始获取....);
res.download()

但是我收到标题已发送错误。 / p>

关于如何防止在处理数据和完成文件下载之前超时的任何想法?



预先感谢

解决方案

首先要注意几件事,我认为应该是 res.writeHead()而不是 res.writeHeader()。其次,一旦发送了标题,就无法再次发送它们。您收到错误消息是因为 res.download()使用 res.sendFile()会重新发送标头。您不能使用 res.download() res.sendFile() res .send(),因为这三种方法都隐式设置了标头并作为响应的一部分进行发送。您需要结合使用 res.writeHead() res.write() res.end()。我只是GET,所以我可以从浏览器中对其进行测试,显然可以使用post。

  router.get('/ test ',函数(req,res,next){
res.writeHead(200,{'Content-Type':'application / json',
'Content-Disposition':'attachment',
'filename':'package.json'
});

fs.readFile( package.json,function(err,data){
if(err ){
throw err;
} else {
setTimeout(function(){
res.write(data);
res.end()
},200000);

}


});

函数keepBusy(){
if(!res .finished){
res.writeProcessing();
setTimeout(keepBusy,1000)
}

}

setTimeout(keepBusy, 1000);

});

这对我来说很好。 res.writeProcessing()发送 102 状态代码。建议持续时间超过20秒,但博客帖子指出

的请求


102已​​被遗漏了HTTP规范的最新版本


,但在大多数浏览器中对我来说效果很好。如果您对客户端也有一定的控制权,那么可以找到一种更好,更可靠的解决方案。


I have a POST call to my nodeJS server that searches for some data on the mongo database and returns a CSV file with the requested data. The problem is that the data search and processing exceeds the 2 minute nodeJS default timeout.

On a diferent scenario y used:

res.writeHeader(200,'application/json');            
res.write('starting fetch .... ');

to keep alive the request and prevent the timeout from the client by sending some res.write(''); from time to time.

Now Im using res.download() to download the resulting csv file, so not just sending JSON as response.

Tried to use this solution like this:

res.writeHeader(200,'application/json');            
res.write('starting fetch .... ');
res.download()

But i get the "headers already sent" error.

Any idea on how to prevent the timeout until the data is processed and the file download is done ?

Thanks in advance

解决方案

A few small things first of all, i think it's supposed to be res.writeHead() instead of res.writeHeader(). Secondly once you send the headers you can not send them again. You are getting the error because res.download() uses res.sendFile() which re-sends the headers. You can't use res.download() or res.sendFile() or res.send() because all of these three methods implicitly set the headers and send as part of the response. You'll need to use a combination of res.writeHead() ,res.write() and res.end(). I am just GET so i can test it from the browser, you can use post obviously.

router.get('/test', function (req,res,next) {
 res.writeHead(200, {'Content-Type': 'application/json',
         'Content-Disposition': 'attachment',
         'filename':'package.json'
 });

 fs.readFile("package.json",function (err,data) {
     if (err) {
         throw err;
     }else{
         setTimeout(function () {
             res.write(data);
             res.end()
         },200000);

     }


 });

 function keepBusy(){
     if(!res.finished){
         res.writeProcessing();
         setTimeout(keepBusy,1000)
     }

 }

 setTimeout(keepBusy,1000);

});

This works fine for me. res.writeProcessing() sends the 102 status code. It is recommended for requests that last greater than 20 second but this blog post says that

102 has been left out more recent revisions of HTTP specifications

but it works fine for me in most browsers. If you have some control over the client as well there can be a much better and reliable solution.

这篇关于nodeJS防止res.download超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆