npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

batch-api-requests

v0.0.1

Published

Nodejs Client and Express module for handling batch HTTP requests.

Downloads

7

Readme

batch-api-requests

Module for doing parallel JSON HTTP requests with a single, persistent connection. The module includes both a Node.js client and Express 4 server middleware.

It's loosely based on the implementation described in the oData Specification and Google Batch Requests.

Client

Basic usage:

var batchClient = require('batch-api-requests').client,
	batchConnection = batchClient.connect({
		url: 'https://api.someserver.com/batch',
		headers: {
			Authorization: 'Bearer {{someToken}}''
		}
	}),
	items = [// an array of request objects
		{
            method: 'POST',
            path: '/test1',
            body: {
				name: 'John'
        	}
        }, {
            method: 'POST',
            path: '/test1',
            body: {
				name: 'Peter'
        	}
        }
	];
	
	// sending all items
	items.forEach(function (item) {
		batchConnection.send(item, function (error, body, response) {
            // handle item response...
            //
            // body: String
			// response: {
			//      statusCode: Number,
			//      statusMessage: String,
			//      headers: {}, 
			// }
        });
	});

	batchConnection.close();

Remember to call the close method

The last items may not be sent until you close the connection.

Once the connection has been set as closed all consequent send calls will return an error and no message will be sent.

The connection may be unexpectedly dropped by the remote server. In that case some of the pending requests will be ignored and errors will be returned. There is the chance that the request has already been sent and processed by the remote server but we didn't receive the response due to the dropped connection. Any following send command will re-open the connection.

The callback on the send method is optional;

Server

The middleware will act as a proxy, issuing an HTTP request for each one of the batched requests.

Basic usage:

var batchMiddleware = require('batch-api-requests').server,
	express = require('express'),
 	app = express(),
 	options = {};// check available options and it's default values

app.use('/batch', batchMiddleware(options));

options.logger

A logger object with the following methods:

  • error
  • info
  • warn

If no logger is provided console will be used.

Setting it as false will disable any reporting.

options.parallelLimit

default value: 10

Maximum parallel requests that will be performed by the middleware, for each connection. This allows us to control the stress on the application servers.

options.retries

default value: 0

Setting this value will force a number of re-tries. All internal errors will be logged but only the last one will be returned to the client.

options.protocol

The protocol to be used. By default 'https'.

options.timeout

Default value 5000.

This is the timeout for the proxied request, not the batch connection. Each batch request will be performed internally on the server/datacenter, so a small timeout is acceptable.