如果NodeJS的function花费太长时间,是否会超时?

我有一些第三方API运行的function,大约需要30分钟才能返回结果(他们做了一些冗长的计算)。 现在,如果我使用Nodejs调用这样一个适当的callbackAPI,默认情况下,Nodejs会等待30分钟的时间来返回结果,还是会超时? 如果超时,有没有办法增加等待时间?

Linux的默认值可以使用20s – 120s之间的任何值作为超时值。http://www.sekuda.com/overriding_the_default_linux_kernel_20_second_tcp_socket_connect_timeout

根据您正在使用的请求库,您可能在设置超时时使用不同的语法。 我个人喜欢request很多,请参考这里https://github.com/request/request

 request.get('http://10.255.255.1', {timeout: 1500}, function(err) { console.log(err.code === 'ETIMEDOUT'); // Set to `true` if the timeout was a connection timeout, `false` or // `undefined` otherwise. console.log(err.connect === true); process.exit(0); }); 

我不认为NodeJSfunction本身会超时。

我假设你正在使用express如果你使用快递,你可以添加下面的代码来增加等待时间。

 var server = app.listen(app.get('port'), function() { debug('Express server listening on port ' + server.address().port); }); server.timeout = 1000; 

否则你可以使用纯粹的http

 var http = require('http'); var server = http.createServer(function (req, res) { setTimeout(function() { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }, 200); }).listen(1337, '127.0.0.1'); server.timeout = 20;