kenneth koontz
kenneth koontz

Reputation: 869

How to perform several http requests with node.js

I need to perform several http requests. I have an array of urls, when each request completes the JSON object is pushed into an array. When all the requests finish, the JSON object is sent to the client with the newly constructed Array. If I was doing this client side, I would use something like jQuery's Deferred object. Is there another approach using node.js without having to import jQuery's Deferred object? Doesn't seem like event emitter would be used to handle this. Or would it? Thanks!

Upvotes: 0

Views: 909

Answers (2)

ace
ace

Reputation: 7583

Here's a possible solution.

This class extends EventEmitter. What it does is for every asynchronous request response, we emit the count of finished asynchronous requests and store the body of the response.

var UrlHandler, events, request, urlHandler,
  __hasProp = {}.hasOwnProperty,
  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

events = require("events");
request = require("request");

UrlHandler = (function(_super) {
  __extends(UrlHandler, _super);

  function UrlHandler() {
    return UrlHandler.__super__.constructor.apply(this, arguments);
  }

  UrlHandler.prototype.getUrls = function(urls) {
    var processedUrls, url, urlhandler, _i, _len, _results;
    processedUrls = 0;
    urlhandler = this;
    urlhandler.urlResponse = [];
    _results = [];
    for (_i = 0, _len = urls.length; _i < _len; _i++) {
      url = urls[_i];
      _results.push(request({
        uri: url
      }, function(error, response, body) {
        urlhandler.urlResponse.push(response);
        processedUrls++;
        return urlhandler.emit("processed:" + processedUrls);
      }));
    }
    return _results;
  };

  return UrlHandler;

})(events.EventEmitter);

urlHandler = new UrlHandler();

module.exports = {
  urlHandler: urlHandler
};

Then in your main app, we put the listener as 'processed:' + urls.length so it will trigger once it's done with all asynchronous requests.

urls = ['http://stackoverflow.com/questions/12739605/how-to-perform-several-http-requests-with-node-js', 'http://stackoverflow.com'];

UrlHandler.getUrls(urls);
UrlHandler.once("processed:" + urls.length, function() {
  return res.json(UrlHandler.urlResponse);
});

Upvotes: 0

jwchang
jwchang

Reputation: 10864

Make sure you allow multiple connections in Node.js first

require('http').globalAgent.maxSockets = 1000

This snippet allows your Node.js server to accept 1000 http requests per client.


There are many flow control libraries in Node.js.

Famous ones are async and step.

Personally I prefer async, because It has functions similar to underscore or lodash but async versions of it.

Upvotes: 2

Related Questions