Standardized JSON response utility for Express with TypeScript support.
npm install express-json-responderA lightweight and consistent utility for sending structured JSON responses in Express.js applications using TypeScript.
- β
Standardized response format
- βοΈ Works with http-status-codes
- π¬ Custom and fallback messages
- β Built-in support for error responses
- π¦ Easy to integrate with any Express project
- π§ͺ TypeScript ready
---
````
npm install express-json-response http-status-codes
---
`ts`
import jsonResponse from "express-json-response";
import { StatusCodes } from "http-status-codes";
`ts`
jsonResponse(StatusCodes.OK, res, { user: userData }, "User fetched successfully");
`ts`
jsonResponse(StatusCodes.BAD_REQUEST, res, null, "Invalid data", {
email: "Email format is incorrect",
password: "Password is too short"
});
---
All responses follow a consistent structure:
`json`
{
"success": true,
"status": "success",
"code": 200,
"message": "Request successful",
"data": {
"user": { "id": 1, "name": "Lateef" }
}
}
`json`
{
"success": false,
"status": "error",
"code": 400,
"message": "Invalid data",
"errors": {
"email": "Email format is incorrect",
"password": "Password is too short"
}
}
---
Fallback messages are used if you donβt provide a message argument:
| HTTP Code | Default Message |
| --------- | ---------------------- |
| 200 | Request successful |
| 201 | Request successful |
| 202 | Request accepted |
| 400 | Invalid request data |
| 401 | Unauthorized |
| 403 | Access forbidden |
| 404 | Resource not found |
| 408 | Request timed out |
| 422 | Unprocessable entity |
| 500 | Something went wrong |
| 503 | Service is unavailable |
---
`ts`
jsonResponse(
code: number,
res: Response,
data?: any,
message?: string,
errors?: Record
): void
* code: HTTP status code (e.g. StatusCodes.OK)res
* : Express Response objectdata
* : Optional data object (used on success)message
* : Optional message stringerrors
* : Optional object with field-specific errors
---
Contributions, suggestions, and improvements are welcome! To contribute:
1. Fork the repository
2. Create a new branch: git checkout -b feature/your-featuregit commit -m "Add your feature"
3. Commit your changes: git push origin feature/your-feature
4. Push to the branch:
5. Open a pull request
---
MIT License Β© Abdullateef Mubarak
``