Skip to content

DevRGD/server-response-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Server Response Helper

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.

NPM Version NPM Downloads Build Status License


πŸ€” Why server-response-helper?

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

πŸš€ Installation

npm install server-response-helper

πŸ’‘ Usage

1. Register the Middleware

Import 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);

2. Send Standardized Responses

🏷️ 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" }

πŸ“š API Reference

responseHelper()

Express middleware that extends res with .response().

res.response(status, [messageOrIndex], [data], [path])

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

🀝 Contributing

We welcome contributions! Here's how to get started:

1. Clone

git clone https://github.com/DevRGD/server-response-helper.git cd server-response-helper

2. Install dependencies

npm ci

3. Run tests

npm test

4. Lint and format before pushing

npm run lint npm run format

5. Build for publishing

npm run build

Then push your feature branch and open a PR.


πŸ“œ License

MIT Β© DevRGD. See the LICENSE file.