http-status-constants-js
v1.0.0
Published
HTTP status code constants for REST APIs
Downloads
95
Maintainers
Readme
http-status-constants-js
A lightweight, zero-dependency HTTP status code constants library for REST APIs.
This package provides immutable HTTP status codes as named constants to help you avoid magic numbers and keep your API code clean and readable.
Installation
npm install http-status-constants-jsModule Support
This package supports both CommonJS and ES Module projects.
CommonJS usage
const { StatusCodes } = require("http-status-constants-js");
res.status(StatusCodes.OK).json({ message: "Success" });ES Module usage
import { StatusCodes } from "http-status-constants-js";
res.status(StatusCodes.CREATED).json({ message: "Resource created" });Basic Example (Express.js)
import express from "express";
import { StatusCodes } from "http-status-constants-js";
const app = express();
app.get("/users/:id", (req, res) => {
const user = null;
if (!user) {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: "User not found" });
}
res.status(StatusCodes.OK).json(user);
});
app.listen(3000);Available Status Codes
Informational (1xx)
CONTINUE(100)SWITCHING_PROTOCOLS(101)PROCESSING(102)
Success (2xx)
OK(200)CREATED(201)ACCEPTED(202)NON_AUTHORITATIVE_INFORMATION(203)NO_CONTENT(204)RESET_CONTENT(205)PARTIAL_CONTENT(206)
Redirection (3xx)
MULTIPLE_CHOICES(300)MOVED_PERMANENTLY(301)FOUND(302)SEE_OTHER(303)NOT_MODIFIED(304)TEMPORARY_REDIRECT(307)PERMANENT_REDIRECT(308)
Client Errors (4xx)
BAD_REQUEST(400)UNAUTHORIZED(401)PAYMENT_REQUIRED(402)FORBIDDEN(403)NOT_FOUND(404)METHOD_NOT_ALLOWED(405)NOT_ACCEPTABLE(406)REQUEST_TIMEOUT(408)CONFLICT(409)GONE(410)UNSUPPORTED_MEDIA_TYPE(415)TOO_MANY_REQUESTS(429)
Server Errors (5xx)
INTERNAL_SERVER_ERROR(500)NOT_IMPLEMENTED(501)BAD_GATEWAY(502)SERVICE_UNAVAILABLE(503)GATEWAY_TIMEOUT(504)
