简体   繁体   English

通过Node.js进行多个API连接

[英]Multiple API connections through Nodejs

I want to connect to theMovieDb api, and fetch a large number of movies. 我想连接到MovieDb API,并获取大量电影。 The api limit is 40 calls every 10 seconds and 20 connections per IP. api限制为每10秒40个调用和每个IP 20个连接。

How do I do this in node.js? 如何在node.js中执行此操作? Right now I have one http request to the api at a time, but anything more than 40 calls results in errors. 现在,我一次向api发送一个http请求,但是超过40次调用都会导致错误。

40 requests per 10 second, 20 concurrent connections, so in "one thread" we'll have to wait approximately 5 seconds. 每10秒40个请求,并发20个连接,因此在“一个线程”中,我们将不得不等待大约5秒钟。

There's an awesome library for all async stuff called async . 有一个很棒的库,用于所有异步内容,称为async

var loadOne = function (id, callback) {
   // Do your stuff
   // tell it to wait
   setTimeout(callback, 5000);
} 


async.mapLimit(arrayOfIds, 20, loadOne, function(err, results){
    // results is now an array of stats for each file
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM