Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including http-queue with all npm packages installed. Try it out:

var httpQueue = require("http-queue")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

http-queue v0.2.1

Queue HTTPS requests to, for example, limit the number of API requests made in X amount of time

https://github.com/ChrisWoollon/http-queue/

https://www.npmjs.com/package/http-queue

http://chriswoollon.co.uk/

http-queue

Queue HTTPS requests to, for example, limit the number of API requests made in X amount of time.

I found myself copy+pasting this code into lots of projects, so decided I'd just make it available.

Usage

npm install http-queue
let httpQueue = require('http-queue');
const x = 1000; // ms between requests
httpQueue = new httpQueue(x);

// Runs on init
httpQueue.newRequest('https://google.co.uk', function(data) {
    console.log(data);
});
// Runs 1000ms later
httpQueue.newRequest('https://github.com', function(data) {
    console.log(data);
});

Alternatively, newRequest takes an object as its first parameter;

var options = {
    url: https://jsonplaceholder.typicode.com/posts/1
}
queue.newRequest(options, function(data) {
    console.log(data);
});

You can also pass functions as variables for multi-use, and handle errors in their own function

let onResponse = function(data) {
    console.log(data);
}
let error = function(err) {
    console.log(err);
    // Do something with the error...
}
httpQueue.newRequest('https://twitter.com', onResponse, error);

Parameters:

ParameterExample
url'http://example.com/'
method'GET'
headers{ Content-Type': 'application/json' }
bodyJSON data
Content-Type supported:
application/json
application/x-www-form-urlencoded

You can also define the URL in its constituent parts, e.g.

ParameterExample
options.protocol'http'
options.hostname'example.com'
options.host'example.com:8888'
options.path'/get/the/path/'
options.port8888

Contribute

Have a feature request, spotted an issue? Get in touch or make a pull request: https://github.com/ChrisWoollon/http-queue

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free