A comprehensive utility package for handling HTTP responses in Node.js applications. This package provides a set of methods to standardize API responses across your application, making it easier to maintain consistent response formats.
You can install this package using npm:
npm install http-response-handler
Here's a basic example of how to use the responseHandeler:
import responseHandler from 'http-response-handler';
// Example usage in an Express.js route
app.get('/api/example', (req, res) => {
try {
// Your logic here
const data = { message: 'Success' };
responseHandler.success(res, 'Operation successful', data);
} catch (error) {
responseHandler.error(res, 'An error occurred');
}
});
The responseHandeler
object provides the following methods:
Sends a successful response.
-
res
: The response object from Express.js -
message
: A success message -
data
: The data to be sent in the response -
statusCode
: HTTP status code (default: 200)
Example:
responseHandler.success(res, 'User created successfully', { userId: 123 });
Sends a general error response.
-
res
: The response object from Express.js -
message
: An error message -
statusCode
: HTTP status code (default: 500)
Sends a validation error response.
Sends an unauthorized error response.
Sends a forbidden error response.
Sends a not found error response.
Sends a conflict error response.
Sends an internal server error response.
Sends a bad request error response.
Sends an unprocessable entity error response.
Sends a too many requests error response.
Sends a service unavailable error response.
Sends a gateway timeout error response.
Example usage for error responses:
responseHandler.notFound(res, 'User not found');
responseHandler.unauthorized(res, 'Invalid credentials');
responseHandler.badRequest(res, 'Invalid input data');
All responses follow a consistent format:
{
"success": true,
"message": "Success message",
"data": { ... }
}
{
"success": false,
"message": "Error message"
}
- Use the appropriate method for each scenario to ensure consistent status codes.
- Provide clear and informative messages in your responses.
- For success responses, always include relevant data in the
data
field. - Handle errors gracefully and use the specific error methods when possible.
app.get('/api/users/:id', (req, res) => {
const user = getUserById(req.params.id);
if (user) {
responseHandler.success(res, 'User retrieved successfully', user);
} else {
responseHandler.notFound(res, 'User not found');
}
});
app.post('/api/users', (req, res) => {
const { username, email } = req.body;
if (!username || !email) {
responseHandler.validationError(res, 'Username and email are required');
return;
}
// Process the request...
});
app.get('/api/admin', (req, res) => {
if (!req.user.isAdmin) {
responseHandler.unauthorized(res, 'Admin access required');
return;
}
// Process the request...
});
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- Initial release with basic response handling methods.
If you encounter any issues or have questions, please file an issue on the GitHub repository.
Thanks to all contributors who have helped to improve this package.