http-response-text
v0.6.4
Published
In-takes HTTP status code and language code (default to 'en'), spits out human-readable response text.
Downloads
47
Maintainers
Readme
http-response-text
In-takes HTTP status code and language code (default to 'en'), spits out response object with corresponding human-readable text message.
Based on MDN web docs for HTTP response status codes.
Installation
- PNPM
pnpm add http-response-text - NPM
npm install http-response-text
Usage
Import the getStatus method and call it with a numeric status code.
For example, the following code snippet validates a user input object id for MongoDB and return an 404 response object for non-valid payload:
import { MongoClient, ObjectId } from "mongodb";
import { getStatus } from "http-response-text";
const findById = async (collectionName, id) => {
// console.log("findById: ", collectionName, id);
if (!ObjectId.isValid(id)) return getStatus(404) ?? null;
// ...
};Which would return you a response object like this:
{
code: 404,
message: 'Not Found'
}