[!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 i express-response-middleware
yarn add express-response-middleware
pnpm i express-response-middleware
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)
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
})
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
})
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
})
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
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. ifres.end
has been called), it will abort. - Calling
res.json
orres.send
fromwriteMiddleware
can lead to unexpected behavior since they end the response internally. - The returned value of
res.write
will be inaccurate when usingwriteMiddleware
, beware if you rely on it -
res.end
afterres.write
would not trigger endMiddleware, as header already sent
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
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
pnpm i
pnpm test
pnpm build
This repo uses Release Please to release.
- Merge your changes into the
main
branch. - 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!