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

v2.2.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, or res.write methods.

NPM GitHub CI Conventional Commits npm type definitions

Installation | Quick Start | API | Contributing

📦 Installation

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 end.json, 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 end.write, allow transform buffer.

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

const myMiddleware = writeMiddleware((chunk, encoding, req, res) => {
    // your code here
    return chunk
})

[!CAUTION] Promise callback support is limited, it doesn't resolve multiple write call yet Code

  • 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 can lead to unexpected behavior since they end the response internally.
  • The returned value of res.write will be inaccurate when using writeMiddleware, beware if you rely on it
  • 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

TODO

Current state project is up to modern standard and support all of the express-mung use case, here is the list that I think can improve on.

  • [ ] Support multiple write call with writeMiddleware + async handler

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!