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-response-middleware

v3.0.0

Published

Transform an express response

Readme

Express Response Middleware

[!NOTE]
Come from express-mung? Checkout the Migration Guide

Middleware for express responses. Fork of express-mung

This package allows synchronous and asynchronous transformation of an express response. This is a similar concept to the express middleware for a request but for a response. Note that the middleware is executed in LIFO order. It is implemented by monkey patching (hooking) the res.end, res.json, res.jsonp, res.send, or res.write methods.

NPM GitHub CI Conventional Commits npm type definitions

Installation | Quick Start | API | Contributing

📦 Installation

Requires Node.js 22.12 or later. The package ships as ESM only. CommonJS projects can still load it with require(), which Node.js supports for ESM from 22.12 on.

NPM

npm i express-response-middleware

Yarn

yarn add express-response-middleware

PNPM

pnpm i express-response-middleware

🚀 Quick Start

Sample middleware (redact.js) to remove classified information.

import { jsonMiddleware } from 'express-response-middleware';

/* Remove any classified information from the response. */
export const hideSecretMiddleware = jsonMiddleware((body, req, res) => {
  if (body.secret) body.secret = '****';
  // ...
  return body;
});

then add to your app.js file (before the route handling middleware)

app.use(hideSecretMiddleware);

💻 API

jsonMiddleware

Intercept res.json, allow transform the JSON body of the response.

import { jsonMiddleware } from 'express-response-middleware';

const myMiddleware = jsonMiddleware((json, req, res) => {
  // your code here
  return json;
});

jsonpMiddleware

Intercept res.jsonp, allow transform the JSON body of the response.

import { jsonpMiddleware } from 'express-response-middleware';

const myMiddleware = jsonpMiddleware((json, req, res) => {
  // your code here
  return json;
});

sendMiddleware

Intercept res.send, allow transform the body of the response.

import { sendMiddleware } from 'express-response-middleware';

const myMiddleware = sendMiddleware((payload, req, res) => {
  // your code here
  return payload;
});

endMiddleware

Intercept res.end, allow transform the HTTP headers of the response.

import { endMiddleware } from 'express-response-middleware';

const myMiddleware = endMiddleware((req, res) => {
  // your code here
});

[!CAUTION] Sending a response while in endMiddleware* is undefined behaviour and will most likely result in an error

writeMiddleware

Intercept res.write and res.end, allow transform each chunk of the body. Chunks are piped through a Node.js Transform stream, so async callbacks are processed in order and backpressure is respected.

import { writeMiddleware } from 'express-response-middleware';

const myMiddleware = writeMiddleware((chunk, encoding, req, res) => {
  // your code here
  return chunk;
});
  • When writeMiddleware detects that a response has completed (i.e. if res.end has been called), it will abort.
  • Calling res.json or res.send from writeMiddleware ends the response; the remaining chunks are dropped.
  • Writes are flushed asynchronously, so res.headersSent may still be false right after res.write.
  • Content-Length is removed since the transformed body length is unknown; the response uses chunked encoding.
  • Chunk boundaries are not guaranteed: chunks may be merged as they pass through the stream.
  • res.end after res.write would not trigger endMiddleware, as header already sent

Exception handling

responseMiddleware catches any exception (synchronous, asynchronous or Promise reject) and sends an HTTP 500 response with the exception message. You should handle your own error if you want different behavior

🤝 Contributing

Development

Local Development

pnpm i
pnpm test

Build

pnpm build

Release

This repo uses Release Please to release.

To release a new version

  1. Merge your changes into the main branch.
  2. An automated GitHub Action will run, triggering the creation of a Release PR.
  3. Merge the release PR.
  4. Wait for the second GitHub Action to run automatically.
  5. Congratulations, you're all set!