如何修改nodejs请求的默认超时时间? [英] How to modify the nodejs request default timeout time?

查看:2580
本文介绍了如何修改nodejs请求的默认超时时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个Node / express服务器。 express的默认超时时间为12万ms,但对我来说还不够。当我的响应达到120,000 ms时,控制台将记录 POST / additem 200 120006ms ,该页面显示错误,因此我想将超时设置为更大的值。我如何做?

解决方案

我假设你正在使用 express ,给定你在你的问题的日志。关键是在服务器上设置超时属性(以下将超时设置为1秒,使用您想要的任何值):

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

如果您不使用express,只能使用香草节点,原则是一样的。以下不会返回数据:

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

server.timeout = 20;
console.log('Server running at http://127.0.0.1:1337/');


I'm using a Node/express server. The default timeout of express is 120,000 ms, but it is not enough for me. When my response reaches 120,000 ms, the console will log POST /additem 200 120006ms and the page shows an error, so I want to set the timeout to a larger value. How would I do that?

解决方案

I'm assuming you're using express, given the logs you have in your question. The key is to set the timeout property on server (the following sets the timeout to one second, use whatever value you want):

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

If you're not using express and are only working with vanilla node, the principle is the same. The following will not return data:

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;
console.log('Server running at http://127.0.0.1:1337/');

这篇关于如何修改nodejs请求的默认超时时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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