1

I'm using the node.js request module to fetch the contents of URLs. it works great, and I can even pass in maxSockets: XXX to have it do more in parallel.

However, I have a few places in my code where I need to do different requests to different places. I'd like to be able to create two separate pools of 25 requests at a time, etc. However, the request module doesn't seem set up to have a constructor to create different pools.

Suggestions for the how I might go about this?

I'd ideally like something along the following lines:

var r1 = new request({ maxSockets: 25 });
var r2 = new request({ maxSockets: 25 });

and then just start using them both for fetching stuff …

Any suggestions how to go about this?

1 Answer 1

1

I'm not sure if I get your question right - maybe because I've never used the request module.

But, by reading the module README, I think you could, in some file of your project, export a function which defaults some options for you.
Somewhat like this:

function createRequest( url, callback ) {
  return request({
    pool: { maxSockets: 25 },
    // Maybe more things here
    url: url
  }, callback );
}

Hope it helps you

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion. However, what I understand about node.js namespaces, however, is that since they're just referring to the same require()d module, they'll all get the same object (module) instance, yes? I want separate object instances and pools.
Node only caches the evaluated file exports when you do require(); so, in your case, the request module is exporting a function. This function will create everything internally when you call it. When you export a single object for example, like a configuration-only file, then you should be careful; probably wrapping it in a function would be better. Hope I clarified things for you now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.