Node Js :是否可以从节点 js 服务器向某些 API 发出 1000 http get 请求
Node Js :is it possible to hit 1000 http get request to some api from a node js server
我试图同时执行近 2000+ 个对某个 Web API 的 http get 请求(abc.com/query?val=somekey
async.each(keysArray,function(key,callback1){
sails.http.get({
hostname:'abc.com',
path:'/query?val='+key,
headers:{
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"User-Agent":"MYBROWSER"
}
},function(response){
var str='';
response.on('data',function(chunk){
str+=chunk;
});
response.on('end',function(){
console.log(new Buffer(str,'utf8'));
// some job on each str from each key
});
});
});
keysArray 的长度大约 2000.So 上面的代码正在执行近 2000 个 http get 请求。但是我遇到了这样的错误
events.js:141
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at exports._errnoException (util.js:870:11)
at TCP.onread (net.js:544:26)
虽然我找到了一种限制并发使用async.eachLimits()
的方法。是否有可能执行那么多请求或机器或服务器的任何类型的依赖关系。
好吧,答案真的很简单,
for-loop
这是逻辑,node.js使用/基于JavaScript,所以它是单线程的(一次一个函数。您不能"从一台机器"执行 1000 个并发请求,但是是的,您可以使用 for 循环发出 1000+ 个请求
for( int i = 0 ; i < 1000 ; i++)
{
// your request code here
}
并记住将您的逻辑包装在异步函数中,然后您可以等待承诺解决,因为您正在寻找 async/await。
const request = require('request-promise')
async function foo (a) {
for (int i = 0 ; i < 1000 ; i++)
try {
let a = await request('localhost:8080/')
// a contains your response data.
} catch (e) {
console.error(e)
}
}
相关文章:
- 向Yahoo Finance API发出JSONP请求时获得400错误请求
- 解决向api.twitter.com发出XMLHttpRequest的方法
- 当鼠标光标纬度等于标记纬度时,发出警报(Google Maps API v3)
- 如何添加api令牌作为我向api发出的每个HTTP请求的用户名
- 通过Node请求向uClassify API发出请求
- 向 Google 日历 API 发出服务器到服务器的请求
- 如何向 Google API 发出经过认证的 RESTful oAuth 2 后端请求
- Node Js :是否可以从节点 js 服务器向某些 API 发出 1000 http get 请求
- $http提供商向 Github API 发出外部请求,但不向 Google 地图 API 发出请求
- 如何向需要身份验证的 API 发出 Javascript JSON 请求
- 向谷歌日历 API 发出 ajax 请求
- 在每秒 10 次调用限制的情况下一次发出 50 个 API 请求的有效方法是什么(烂番茄 API)
- 使用node.js向Google API发出http请求
- 向一个API发出多个请求,该API每分钟只能处理20个请求
- 如何向外部API发出GET和POST请求
- 向Amazon web服务REST api发出请求时,无法使请求签名工作
- 为什么我不能在Angular中向Google Maps API发出AJAX请求?
- 唯一标识向Web API发出请求的客户端的内容
- 如何使用jQuery向Dictionary.com的REST API发出AJAX请求
- 试图在https://api.thetvdb.com/上向TVDB REST API发出JSON POST请求,但得到C