express-response-middleware
v3.0.0
Published
Transform an express response
Maintainers
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.
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-middlewareYarn
yarn add express-response-middlewarePNPM
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
writeMiddlewaredetects that a response has completed (i.e. ifres.endhas been called), it will abort. - Calling
res.jsonorres.sendfromwriteMiddlewareends the response; the remaining chunks are dropped. - Writes are flushed asynchronously, so
res.headersSentmay still befalseright afterres.write. Content-Lengthis 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.endafterres.writewould 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 testBuild
pnpm buildRelease
This repo uses Release Please to release.
To release a new version
- Merge your changes into the
mainbranch. - An automated GitHub Action will run, triggering the creation of a Release PR.
- Merge the release PR.
- Wait for the second GitHub Action to run automatically.
- Congratulations, you're all set!
