An elegant Express middleware that provides a .response() helper to create consistent, standardized JSON responses across your API. Features include custom messages, TypeScript support, and zero dependencies.
In modern API development, consistency is key. Clients consuming your API expect a predictable response structure. This tiny helper enforces a standard JSON response format across all your endpoints, making your API more robust and easier to work with.
- β
Standardize Responses: Uniform structure (
status,message,data,timestamp,path) - π Lightweight & Fast: Zero dependencies, minimal overhead
- β¨ Easy to Use: Plug-and-play setup
- π Fully Typed: Built in TypeScript for safety and autocompletion
npm install server-response-helperImport the default response middleware and register it with Express.
import express from 'express'; import response from 'server-response-helper'; const app = express(); app.use(response);π·οΈ Example A β custom message (string)
app.post('/api/login', (req, res) => { const user = { id: 1, name: 'Gautam' }; res.response(200, 'Login successful!', { user }); });Result:
{ "status": 200, "message": "Login successful!", "data": { "user": { "id": 1, "name": "Gautam" } }, "timestamp": "2025-07-01T01:02:45.000Z", "path": "/api/login" }π·οΈ Example B β default message (number index)
app.get('/api/item/:id', (req, res) => { res.response(404); });Assuming:
statusMessages[404] = ['Not found', 'Nothing here'];Result:
{ "status": 404, "message": "Not found", "timestamp": "2025-07-01T01:02:45.000Z", "path": "/api/item/42" }Express middleware that extends res with .response().
| Parameter | Type | Default | Description |
|---|---|---|---|
status | number | β | Required. HTTP status code (200, 404, etc.) |
messageOrIndex | string | number | 0 | Use a string for a custom message, or a number to index into default messages |
data | any | undefined | Optional payload under data field |
path | string | req.path | Optional override of the path field |
We welcome contributions! Here's how to get started:
git clone https://github.com/DevRGD/server-response-helper.git cd server-response-helpernpm cinpm testnpm run lint npm run formatnpm run buildThen push your feature branch and open a PR.