HTTP status code constants for REST APIs
npm install http-status-constants-jsbash
npm install http-status-constants-js
``
---
Module Support
This package supports both CommonJS and ES Module projects.
$3
`js
const { StatusCodes } = require("http-status-constants-js");
res.status(StatusCodes.OK).json({ message: "Success" });
`
$3
`js
import { StatusCodes } from "http-status-constants-js";
res.status(StatusCodes.CREATED).json({ message: "Resource created" });
`
---
Basic Example (Express.js)
`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
$3
* CONTINUE (100)
* SWITCHING_PROTOCOLS (101)
* PROCESSING (102)
$3
* OK (200)
* CREATED (201)
* ACCEPTED (202)
* NON_AUTHORITATIVE_INFORMATION (203)
* NO_CONTENT (204)
* RESET_CONTENT (205)
* PARTIAL_CONTENT (206)
$3
* MULTIPLE_CHOICES (300)
* MOVED_PERMANENTLY (301)
* FOUND (302)
* SEE_OTHER (303)
* NOT_MODIFIED (304)
* TEMPORARY_REDIRECT (307)
* PERMANENT_REDIRECT (308)
$3
* 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)
$3
* INTERNAL_SERVER_ERROR (500)
* NOT_IMPLEMENTED (501)
* BAD_GATEWAY (502)
* SERVICE_UNAVAILABLE (503)
* GATEWAY_TIMEOUT` (504)