Node js socket explanation

前端 未结 2 1794
孤街浪徒
孤街浪徒 2020-11-28 15:53

I am building an application that will be making about a million calls to a remote api server. Will I be able to limit amount of connections to for example 10? Do I set max

2条回答
  •  猫巷女王i
    2020-11-28 16:48

    I decided to use the async library.

    Here is my complete solution to this:

    var async = require('async')
    
    var http = require('http');
    
    var inputData = [];
    
    for(i=1; i<=2000;i++){
        inputData.push('number' + i);
    }
    
    var options = {
        host: "o2.pl",
        path: "/static/desktop.css?v=0.0.417",
        port: 80
    }
    
    function fetchData(number, callback){
    
        return new Promise(function(resolve, reject){
    
            fetch  = function(resp){
                var body = '';
                resp.on('data',function(chunk){
                    body += chunk;
                })
                process.stdout.write('.')
    
                callback()
    
                resp.on('error',function(err){
                    console.log('error');
                    console.log(err);
    
                })
            }
            var req = http.request(options, fetch);
    
            req.end();
    
        })
    }
    
    function foo(item, callback){
    
        return callback(false, 'foo');
    }
    
    async.mapLimit(inputData,100,fetchData,function(err, result){
        console.log('finished');
    })
    

    Thank you for your help.

提交回复
热议问题