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 🙏

© 2026 – Pkg Stats / Ryan Hefner

express-batch

v0.2.1

Published

Middleware for Express that allows for batched API requests.

Readme

express-batch

Build Status Test Coverage Code Climate NPM Downloads License

Description

Middleware for Express 4.x that allows for batched API requests.

It's attached as a handler for a particular route.

If you need to perform several different requests to one API simultaneously, you could combine them all together (in one querystring) and send only one request to the handler's route.

The handler parses requests, and then invokes the relevant handler for each request (the standard app router is used), collects all the responses and sends them back as a JSON object with sections for each response.

Currently, only routes for GET locations are supported.

Example

// app init
var express = require("express");
var expressBatch = require("express-batch");
var app = express();

// mount batch middeleware
app.use("/api/batch", expressBatch(app));


// mount ordinary API endpoints
app.get("/api/constants/pi", function apiUserHandler(req, res) {
    res.send(Math.PI);
});

app.get("/api/users/:id", function apiUserHandler(req, res) {
    res.json({
        id: req.params.id,
        name: "Alice"
    });
});

// start the app
app.listen(3000);

This example in code.

With this example, a request to http://localhost:3000/api/batch?users=/api/users/49&pi=api/constants/pi&nonexistent=/not/existent/route will return:

{
    users: {
        result: {
            id: "49",
            name: "Alice"
        },
        status: 200
    },
    pi: {
        result: 3.141592653589793,
        status: 200
    },
    nonexistent: {
        result: "Not Found",
        status: 404
    }
}

It is also possible to have nested field-value pairs by passing in an options argument with a custom separator property.

// mount batch handler with optional separator for nested field-value pairs
var options = {
    separator: ';'
};
app.use("/api/batch", expressBatch(app, options));

// easily handle batched requests with deep field-value pairs
app.get("/api/climate/", function apiClimateHandler(req, res) {
    var response = {
        sunny: false,
        warm: false
    };

    // e.g., with a request path of 'api/batch?climate=/api/climate/?sunny=true&warm=true'
    if (req.query.sunny === 'true' && req.query.warm === 'true') {
        response.sunny = true;
        response.warm = true;
    }
    res.json(response);
});

Limitations

  • Tested only with Express 4
  • Supports only routes for GET requests.
  • Handlers which will bу used beyond the middleware, could use only these methods of response:
    • res.json()
    • res.jsonp()
    • res.jsonp()
    • res.end()
    • res.status()
    • res.sendStatus()
    • res.sendStatus()
    • res.setHeader()
    • assign value to res.statusCode

Notes

There are similar packages, but which work using real http requests:

Todo

  • [x] Returning headers in batch results

  • [ ] Add documentation about headers passing

  • [ ] Support of arrays (batch?users=/api/users/1&users=/api/users/2 should return users: [{id:1}, {id:2}])

  • [ ] Support of rest of HTTP methods

  • [ ] Support of rest of response methods

License

MIT

============= Gitter Bitdeli Badge