2.0.2 • Published 8 months ago

reshttp v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

reshttp - HTTP Status Codes and Messages

reshttp is a comprehensive object that contains HTTP status codes and their professional messages. This can be used across your Node.js/Express application to standardize error handling and responses in a formal and consistent manner.

Table of Contents

Overview

The reshttp object includes all major HTTP status codes, from 1xx (Informational) to 5xx (Server Errors), with associated professional messages. These messages are designed to provide clarity and helpful information to both users and developers.

Installation

NPM

npm install reshttp

PNPM

pnpm add reshttp

YARN

yarn add reshttp

BUN

bun add reshttp

Usage

import express from "express";
import reshttp from "reshttp";


console.log(reshttp.okMessage)
console.log(reshttp.okCode)

const app = express();

app.get("/example", (req, res) => {
  if (!req.query.id) {
    return res.status(reshttp.badRequestCode).json({status: reshttp.badRequestCode, message: reshttp.badRequestMessage});
  }
  res.status(reshttp.okCode).send(reshttp.okMessage);
});

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

In this example:

  • If id is missing in the query, it returns a 400 Bad Request with a formal message.
  • If id is provided, it returns a 200 OK response.

Informational Responses (1xx)

CodeMessage
100Continue: The server has received the request headers and the client may proceed with the request body.
101Switching Protocols: The requester has asked the server to switch protocols.
102Processing: The server has received the request and is processing it, but no response is available yet.
103Early Hints: The server is sending preliminary headers before the final response.

Success Responses (2xx)

CodeMessage
200OK: The request was successful.
201Created: The request was successful, and a new resource was created.
202Accepted: The request has been received but has not yet been acted upon.
203Non-Authoritative Information: The response contains modified metadata not from the origin server.
204No Content: The request was successful, but there is no content to send in the response.
205Reset Content: The server has fulfilled the request and instructs the client to reset the document view.
206Partial Content: The server is delivering only part of the resource due to a range header sent by the client.

Redirection Responses (3xx)

CodeMessage
300Multiple Choices: The request has multiple possible responses.
301Moved Permanently: The resource has been moved permanently to a new URL.
302Found: The resource is temporarily located at a different URL.
303See Other: The client should retrieve the resource at another URL with a GET request.
304Not Modified: The resource has not been modified since the last request.
307Temporary Redirect: The resource is temporarily located at a different URL, but the original method should be used.
308Permanent Redirect: The resource has been permanently moved to a new URL, and the original method should be used.

Client Error Responses (4xx)

CodeMessage
400Bad Request: The server could not understand the request due to invalid syntax.
401Unauthorized: The client must authenticate itself to get the requested response.
402Payment Required: Payment is required to access the resource.
403Forbidden: The client does not have permission to access the requested resource.
404Not Found: The requested resource was not found on the server.
405Method Not Allowed: The request method is not allowed for the requested resource.
406Not Acceptable: The server cannot produce a response matching the accept headers sent in the request.
409Conflict: The request could not be processed due to a conflict with the current state of the resource.
410Gone: The requested resource is no longer available on the server.
411Length Required: The server requires a Content-Length header field in the request.
418I'm a Teapot: The server is a teapot and is not able to brew coffee.
429Too Many Requests: The client has sent too many requests in a given amount of time.

Server Error Responses (5xx)

CodeMessage
500Internal Server Error: The server encountered an unexpected condition that prevented it from fulfilling the request.
501Not Implemented: The server does not support the functionality required to fulfill the request.
502Bad Gateway: The server received an invalid response from the upstream server.
503Service Unavailable: The server is currently unable to handle the request due to maintenance or overload.
504Gateway Timeout: The server did not receive a timely response from the upstream server.

Usage Example

NOTE: it will work with any JAVASCRIPT framework

Here’s an example of how to use the reshttp object in an Express.js route:

Conclusion

The reshttp object helps you keep your status codes and messages organized in a consistent, professional format. You can easily expand or modify the messages as needed for your application.

Feel free to use this structure for error handling, success responses, and status codes throughout your project!


Let me know if you need further customization or have additional requests! 😊