Registry / devops / express-response-formatter

express-response-formatter

JSON →
library2.0.2jsnpmunverified

Express middleware that enhances the response object with a `formatter` property providing readable methods like `ok()`, `created()`, `badRequest()`, etc. Responses are automatically structured: success (2xx/3xx) returns `{ data: ... }`, client/server errors (4xx/5xx) return `{ error: ... }`, and an optional second parameter adds a `meta` field. Version 2.0.2 (latest) ships with TypeScript declarations. It follows a simple, opinionated format and is intended for projects wanting consistent JSON API responses without heavy frameworks. Release cadence is intermittent; maintained as of 2023.

npm install express-response-formatter
INSTALL
IMPORT
SIG · EXPRESS-RESPONSE-F
E
express-response-formatter
devopsjavascriptv2.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

responseEnhancer
import { responseEnhancer } from 'express-response-formatter';
const responseEnhancer = require('express-response-formatter'); // CommonJS works but TypeScript may require default import
ESM and CJS both supported; named export is primary.
ResponseFormatter
import type { ResponseFormatter } from 'express-response-formatter';
import { ResponseFormatter } from 'express-response-formatter'; // This is a type only, runtime import will fail
TypeScript type import; use when extending or typing request handlers.
FormatterResponse
import type { FormatterResponse } from 'express-response-formatter';
const { FormatterResponse } = require('express-response-formatter'); // Not a runtime value
Type for the response object after enhancement.

Demonstrates attaching the middleware and using the ok() and badRequest() formatter methods with metadata.

import express, { Request, Response } from 'express'; import { responseEnhancer } from 'express-response-formatter'; const app = express(); app.use(responseEnhancer()); app.get('/users', (req: Request, res: Response) => { const users = [{ name: 'Alice' }, { name: 'Bob' }]; const meta = { total: 2, page: 1 }; res.formatter.ok(users, meta); }); app.get('/error', (req: Request, res: Response) => { const errors = [{ detail: 'Invalid input' }]; res.formatter.badRequest(errors); }); app.listen(3000, () => console.log('Server running on port 3000'));
Debug
Known issues
gotcharesponseEnhancer() must be called as a function (invoked) before passing to app.use; forgetting parentheses breaks middleware.
fix
Ensure app.use(responseEnhancer()) not app.use(responseEnhancer).
affects: >=1.0.0
gotchaMethods like ok() and badRequest() do not allow passing an empty array or null; they will still send a 200 response with empty data/error field.
fix
Always pass the data/errors argument, even if empty array.
affects: >=1.0.0
gotchaMetadata object passed as second argument overrides response structure if it contains keys 'data' or 'error'; this can break expected format.
fix
Avoid using 'data' or 'error' keys in meta object; they are reserved.
affects: >=1.0.0
deprecatedThe package does not automatically handle content negotiation; it always sets Content-Type to application/json.
fix
If you need other formats, use a separate middleware before this one.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'ok')
responseEnhancer() not applied before route handler, or middleware order incorrect.
fix
Add app.use(responseEnhancer()) before route definitions.
Middleware is not a function
responseEnhancer is not invoked (app.use(responseEnhancer) instead of app.use(responseEnhancer())).
fix
Call responseEnhancer(): app.use(responseEnhancer()).
Property 'formatter' does not exist on type 'Response'
TypeScript type definitions not loaded or incorrect import.
fix
Import types: import { ResponseFormatter } from 'express-response-formatter' and extend Response interface.
data and error are both present in response
Passing meta object with 'data' or 'error' keys that get merged into response.
fix
Ensure meta object does not contain reserved keys 'data' or 'error'.
Upgrade
Version history
2.0.2latest on npm
Audit
Dependencies
expressrequiredpeer dependency; designed to enhance Express response object
Agent activity
4 hits · last 30 days
node
4
Resources
express-response-formatter — npm install express-response-formatter · libregistry