
Security News
Socket Releases Free Certified Patches for Critical vm2 Sandbox Escape
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.
api-response-handler
Advanced tools
A universal API error handler for Node.js applications with standardized responses and error management.
Api Response Handler is a utility library designed for consistent, standardized, and user-friendly API responses in Express.js applications. It simplifies success, error, validation, and pagination responses while reducing repetitive boilerplate code.
Install the package via npm:
npm install api-response-handler
import HandleResponse from 'api-response-handler';
Send a generic success response.
app.get('/success', (req, res) => {
HandleResponse.success(res, 'Data fetched successfully', { id: 1, name: 'Example' });
});
Response:
{
"success": true,
"message": "Data fetched successfully",
"data": {
"id": 1,
"name": "Example"
},
"status": 200
}
Send a success response specifically for resource creation.
app.post('/create', (req, res) => {
HandleResponse.created(res, 'User created successfully', { id: 1 });
});
Response:
{
"success": true,
"message": "User created successfully",
"data": {
"id": 1
},
"status": 201
}
Handle validation failures.
app.post('/validate', (req, res) => {
const errors = { field: 'email', message: 'Invalid email format' };
HandleResponse.validationError(res, 'Validation failed', errors);
});
Response:
{
"success": false,
"message": "Validation failed",
"details": {
"field": "email",
"message": "Invalid email format"
},
"status": 422
}
Handle unauthorized access attempts.
app.get('/unauthorized', (req, res) => {
HandleResponse.unauthorized(res);
});
Response:
{
"success": false,
"message": "Unauthorized Access",
"status": 401
}
Handle forbidden requests.
app.get('/forbidden', (req, res) => {
HandleResponse.forbidden(res);
});
Response:
{
"success": false,
"message": "Forbidden Access",
"status": 403
}
Handle requests to non-existent resources.
app.get('/not-found', (req, res) => {
HandleResponse.notFound(res);
});
Response:
{
"success": false,
"message": "Resource Not Found",
"status": 404
}
Handle unexpected server errors gracefully.
app.get('/server-error', (req, res) => {
try {
throw new Error('Unexpected error');
} catch (error) {
HandleResponse.serverError(res, error);
}
});
Response:
{
"success": false,
"message": "Internal Server Error",
"error": "Unexpected error",
"status": 500
}
Send a structured paginated response.
app.get('/paginated', (req, res) => {
const data = [{ id: 1 }, { id: 2 }];
HandleResponse.paginated(res, data, 1, 10, 50);
});
Response:
{
"success": true,
"message": "Data Retrieved Successfully",
"data": [
{ "id": 1 },
{ "id": 2 }
],
"pagination": {
"page": 1,
"limit": 10,
"total": 50,
"totalPages": 5
},
"status": 200
}
Send a custom error response with details.
app.get('/custom-error', (req, res) => {
HandleResponse.genericError(res, 'Something went wrong', 400, { info: 'Custom issue' });
});
Response:
{
"success": false,
"message": "Something went wrong",
"details": {
"info": "Custom issue"
},
"status": 400
}
Send a 204 No Content response.
app.delete('/delete', (req, res) => {
HandleResponse.noContent(res);
});
Response: (No Body Returned, HTTP Status Code 204)
| Method | Parameters | Description |
|---|---|---|
success | res, message, data, status | Generic success response |
created | res, message, data | Resource creation response |
validationError | res, message, details | Validation error response |
unauthorized | res, message | Unauthorized access |
forbidden | res, message | Forbidden access |
notFound | res, message | Resource not found |
serverError | res, error, message | Server error response |
paginated | res, data, page, limit, total, message | Paginated response |
genericError | res, message, status, details | Custom error response |
noContent | res, message | No content response (204) |
Contributions are always welcome!
This project is licensed under the MIT License.
Pratik Panchal
FAQs
A universal API error handler for Node.js applications with standardized responses and error management.
We found that api-response-handler demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.

Research
Five malicious NuGet packages impersonate Chinese .NET libraries to deploy a stealer targeting browser credentials, crypto wallets, SSH keys, and local files.

Security News
pnpm 11 turns on a 1-day Minimum Release Age and blocks exotic subdeps by default, adding safeguards against fast-moving supply chain attacks.