UNPKG

http-response-helper

Version:

HTTP-Response-Helper is a utility library for sending HTTP responses with the correct status code.

139 lines (98 loc) 3.17 kB
# http-response-helper ![dt](https://img.shields.io/npm/dt/http-response-helper.svg?style=for-the-badge) ![l](https://img.shields.io/npm/l/http-response-helper.svg?style=for-the-badge) ![min](https://img.shields.io/bundlephobia/min/http-response-helper.svg?style=for-the-badge) ![types](https://img.shields.io/npm/types/http-response-helper.svg?style=for-the-badge) ![v](https://img.shields.io/npm/v/http-response-helper.svg?style=for-the-badge) ![npm](https://nodei.co/npm/http-response-helper.png?downloadRank=true&downloads=true) ## Introduction `http-response-helper` is a simple utility library that helps you create HTTP responses with correct status codes and messages. ## Installation ```bash npm install http-response-helper ``` ## Example Usage with Express.js ### 1. Creating a New Item ```javascript 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, () => {}); ``` #### Sample Response for Creating a New Item ```json { "code": 201, "data": {}, "message": "Created" } ``` ### 2. For Getting an Item but Not Found ```javascript 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, () => {}); ``` #### Sample Response for Getting an Item but Not Found ```json { "code": 404, "data": {}, "message": "Item not found." } ``` ## Available HTTP Response Methods ### `1xx`: Informational - Request received, continuing process - CONTINUE - EARLY_HINTS - PROCESSING - SWITCHING_PROTOCOLS - UPLOAD_RESUMPTION_SUPPORTED ### `2xx`: Success - The action was successfully received, understood, and accepted - ACCEPTED - ALREADY_REPORTED - CREATED - IM_USED - MULTI_STATUS - NO_CONTENT - NON_AUTHORITATIVE_INFORMATION - OK - PARTIAL_CONTENT - RESET_CONTENT ### `3xx`: Redirection - Further action must be taken in order to complete the request - FOUND - MOVED_PERMANENTLY - MULTIPLE_CHOICES - NOT_MODIFIED - PERMANENT_REDIRECT - SEE_OTHER - TEMPORARY_REDIRECT ### `4xx`: Client Error - The request contains bad syntax or cannot be fulfilled - BAD_REQUEST - CONFLICT - FORBIDDEN - NOT_FOUND - UNAUTHORIZED ### `5xx`: Server Error - The server failed to fulfill an apparently valid request - BAD_GATEWAY - GATEWAY_TIMEOUT - INTERNAL_SERVER_ERROR - NOT_IMPLEMENTED - SERVICE_UNAVAILABLE ## References - [Hypertext Transfer Protocol (HTTP) Status Code Registry](https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml)