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."
}
- CONTINUE
- EARLY_HINTS
- PROCESSING
- SWITCHING_PROTOCOLS
- UPLOAD_RESUMPTION_SUPPORTED
- ACCEPTED
- ALREADY_REPORTED
- CREATED
- IM_USED
- MULTI_STATUS
- NO_CONTENT
- NON_AUTHORITATIVE_INFORMATION
- OK
- PARTIAL_CONTENT
- RESET_CONTENT
- FOUND
- MOVED_PERMANENTLY
- MULTIPLE_CHOICES
- NOT_MODIFIED
- PERMANENT_REDIRECT
- SEE_OTHER
- TEMPORARY_REDIRECT
- BAD_REQUEST
- CONFLICT
- FORBIDDEN
- NOT_FOUND
- UNAUTHORIZED
- BAD_GATEWAY
- GATEWAY_TIMEOUT
- INTERNAL_SERVER_ERROR
- NOT_IMPLEMENTED
- SERVICE_UNAVAILABLE