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

@naskot/node-express-compose-middleware

v0.1.1

Published

Small Express utility to compose middlewares and a final handler with ctx ({ req, res, next }).

Readme

@naskot/node-express-compose-middleware

Tiny Express-only utility to compose multiple middlewares with a final handler using { req, res, next } context.

In 4 Lines

  • Express-only helper to chain middlewares and final handler in one readable compose(...).
  • Supports both styles: classic (req, res, next) and ctx-style ({ req, res, next }).
  • Unifies async/sync error forwarding to next(err) without extra boilerplate.
  • Works in NestJS only when you use the Express adapter (@nestjs/platform-express).

Why use it

  • Keep middleware chain readable in one function.
  • Final handler can use object context instead of (req, res, next) signature.
  • Same behavior as the starter-template compose.ts.

Install

npm i @naskot/node-express-compose-middleware express

Usage

import { compose } from "@naskot/node-express-compose-middleware";

const routeHandler = compose(authMiddleware("hmac"), async ({ req, res }) => {
  res.json({ ok: true, headers: req.headers, locals: res.locals });
});

Examples

1) Final handler only (ctx style)

import { compose } from "@naskot/node-express-compose-middleware";

export const ping = compose(({ res }) => {
  res.json({ ok: true });
});

2) Final handler only (classic Express style)

import { compose } from "@naskot/node-express-compose-middleware";

export const ping = compose((req, res) => {
  res.json({ method: req.method, ok: true });
});

3) Middleware + final ctx handler

import { compose } from "@naskot/node-express-compose-middleware";

export const targetHMAC = compose(authMiddleware("hmac"), async ({ req, res }) => {
  const remoteRedisKeys = await debugExtractKeysSecretsFromRedis("key");
  const remoteIp = getRemoteIp(req);

  res.json({
    remoteIp,
    remoteRedisKeys,
    ok: true,
    body: req.body,
    query: req.query,
    params: req.params,
    locals: res.locals,
    headers: req.headers,
  });
});

4) Multiple middlewares with res.locals

import { compose } from "@naskot/node-express-compose-middleware";

export const me = compose(
  (_req, res, next) => {
    res.locals.requestId = crypto.randomUUID();
    next();
  },
  async (req, res, next) => {
    res.locals.user = await userService.findByToken(req.headers.authorization ?? "");
    next();
  },
  ({ res }) => {
    res.json({
      requestId: res.locals.requestId,
      user: res.locals.user,
    });
  },
);

5) Access guard (short-circuit)

import { compose } from "@naskot/node-express-compose-middleware";

export const adminStats = compose(
  (_req, res, next) => {
    if (!res.locals.user?.isAdmin) {
      res.status(403).json({ ok: false, error: "forbidden" });
      return;
    }
    next();
  },
  ({ res }) => {
    res.json({ ok: true, scope: "admin" });
  },
);

6) Validation middleware

import { compose } from "@naskot/node-express-compose-middleware";

export const createUser = compose(
  (req, res, next) => {
    if (typeof req.body?.email !== "string" || req.body.email.length === 0) {
      res.status(400).json({ ok: false, error: "email_required" });
      return;
    }
    next();
  },
  async ({ req, res }) => {
    const user = await userService.create({ email: req.body.email });
    res.status(201).json({ ok: true, user });
  },
);

7) Final handler as classic RequestHandler

import { compose } from "@naskot/node-express-compose-middleware";

export const route = compose(
  (_req, _res, next) => next(),
  (_req, _res, next) => next(),
  (req, res) => {
    res.json({ ok: true, path: req.path });
  },
);

8) Throwing errors (auto-forward to next)

import { compose } from "@naskot/node-express-compose-middleware";

export const failDemo = compose(async () => {
  throw new Error("boom");
});

// Express error middleware
app.use((err, _req, res, _next) => {
  res.status(500).json({ ok: false, error: err.message });
});

API

  • compose(handler) where handler is (ctx) => unknown
  • compose(handler) where handler is classic RequestHandler
  • compose(...middlewares, finalHandler) where final handler can be ctx-style or RequestHandler

Local checks

npm run lint
npm run check
npm test
npm run build