如何在expressjs中执行长HTTP请求?

时间:2016-06-29 11:57:09

标签: http express request

我写了一个express.js应用程序,我的一个GET请求需要很长时间才能完成(它在Hadoop客户端上运行一个spark查询)。
由于请求需要花费很长时间,执行请求的客户端(Chrome,Firefox,jQuery)会一次又一次地向express.js应用重新发送相同的请求。
有没有办法处理这些重复的请求?

2 个答案:

答案 0 :(得分:3)

连续提出请求的原因确实是由于请求超时 我设法在请求对象上使用以下内容使其工作:req.connection.setTimeout(60*10*1000);
所以完整的代码看起来像这样:

app.get('/timetunnel/:accountId/:date', (req, res) => {
    // this request takes a lot of time, so increase the timeout
    req.connection.setTimeout(60 * 10 * 1000);
    reallyLongOperation();  // preferably async to prevent server blocking
});

答案 1 :(得分:0)

您可以尝试在app.js文件中添加此内容

var server = app.listen(port);

server.on('connection', function(socket) {
    socket.setTimeout(600 * 60 * 1000);
});

此外,如果您使用任何类型的代理服务器,请确保它在请求时没有超时。