http-response-helper
is a simple utility library that helps you create HTTP responses with correct status codes and messages.
npm install http-response-helper
const { HttpResponseHelper } = require("http-response-helper");
const express = require("express");
const app = express();
app.use(express.json());
app.post("/create-item", (req, res) => {
const data = req.body;
// Logic to create the item
// Send the HTTP response with status code 201 (Created)
HttpResponseHelper.CREATED(res, data);
});
app.listen(3000, () => {});
{
"code": 201,
"data": {},
"message": "Created"
}
const { HttpResponseHelper } = require("http-response-helper");
const express = require("express");
const app = express();
app.use(express.json());
app.get("/not-found", (req, res) => {
const { id } = req.query;
// Logic to find the item (simulate item not found)
// Send 404 response with custom message
HttpResponseHelper.NOT_FOUND(res, { id }, "Item not found.");
});
app.listen(3000, () => {});
{
"code": 404,
"data": {},
"message": "Item not found."
}
1xx
: Informational - Request received, continuing process2xx
: Success - The action was successfully received, understood, and accepted3xx
: Redirection - Further action must be taken in order to complete the request4xx
: Client Error - The request contains bad syntax or cannot be fulfilled5xx
: Server Error - The server failed to fulfill an apparently valid request