如何在 Node.js 中每秒发出一个 http 请求?
How make one http-request per second in Node.js?
那里。我有(例如)100 个链接,我需要为这些资源创建 100 个 http 请求(使用请求模块),但我有一个限制 - 每秒不超过一个请求。你能帮我用 async and request 模块组织这个吗?
更新
我提供了我的工作代码。
var async = require('async');
var request = require('request');
async.concat(links, function (link, callback) {
request(link, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(null, body);
}
});
}, function () {
console.log(results);
});
yes you can eachSeries of async library
async.eachSeries(urlArray, function iterator(url, callback) {
processurl( callback );
}, function done() {
//...
});
您可以使用 underscore.js 中的 throttle 函数来限制调用函数的速率。
那里。我有(例如)100 个链接,我需要为这些资源创建 100 个 http 请求(使用请求模块),但我有一个限制 - 每秒不超过一个请求。你能帮我用 async and request 模块组织这个吗?
更新
我提供了我的工作代码。
var async = require('async');
var request = require('request');
async.concat(links, function (link, callback) {
request(link, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(null, body);
}
});
}, function () {
console.log(results);
});
yes you can eachSeries of async library
async.eachSeries(urlArray, function iterator(url, callback) {
processurl( callback );
}, function done() {
//...
});
您可以使用 underscore.js 中的 throttle 函数来限制调用函数的速率。