Socket
Socket
Sign inDemoInstall

http-status-codes

Package Overview
Dependencies
0
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-status-codes

Constants enumerating the HTTP status codes. Based on the Java Apache HttpStatus API.


Version published
Maintainers
1
Weekly downloads
1,641,587
decreased by-10.01%

Weekly downloads

Package description

What is http-status-codes?

The http-status-codes npm package provides an easy-to-use collection of HTTP status codes and reason phrases, which are useful when writing server-side code. It helps developers to avoid hardcoding numeric status codes and instead use descriptive constants, improving code readability and maintainability.

What are http-status-codes's main functionalities?

Status Code Enumeration

Provides an enumeration of HTTP status codes, allowing developers to use descriptive constants instead of numeric codes.

const { StatusCodes } = require('http-status-codes');

console.log(StatusCodes.OK); // 200
console.log(StatusCodes.NOT_FOUND); // 404
console.log(StatusCodes.INTERNAL_SERVER_ERROR); // 500

Reason Phrase Lookup

Allows developers to get the standard reason phrase for a given HTTP status code.

const { getReasonPhrase } = require('http-status-codes');

console.log(getReasonPhrase(200)); // 'OK'
console.log(getReasonPhrase(404)); // 'Not Found'
console.log(getReasonPhrase(500)); // 'Internal Server Error'

Status Code Lookup

Enables developers to retrieve the numeric status code for a given reason phrase.

const { getStatusCode } = require('http-status-codes');

console.log(getStatusCode('OK')); // 200
console.log(getStatusCode('Not Found')); // 404
console.log(getStatusCode('Internal Server Error')); // 500

Other packages similar to http-status-codes

Readme

Source

http-status-codes

Constants enumerating the HTTP status codes. Based on the Java Apache HttpStatus API.

All status codes defined in RFC1945 (HTTP/1.0), RFC2616 (HTTP/1.1), RFC2518 (WebDAV), RFC6585 (Additional HTTP Status Codes), and RFC7538 (Permanent Redirect) are supported.

TypeScript or JavaScript. Completely library agnostic. No dependencies.

Installation

npm install http-status-codes --save

Usage (express 4.x)

import {
	ReasonPhrases,
	StatusCodes,
	getReasonPhrase,
	getStatusCode,
} from 'http-status-codes';

response
	.status(StatusCodes.OK)
	.send(ReasonPhrases.OK);

response
	.status(StatusCodes.INTERNAL_SERVER_ERROR)
	.send({
		error: getReasonPhrase(StatusCodes.INTERNAL_SERVER_ERROR)
	});

response
	.status(getStatusCode('Internal Server Error'))
	.send({
		error: 'Internal Server Error'
	});

Codes

CodeConstantReason Phrase
100CONTINUEContinue
101SWITCHING_PROTOCOLSSwitching Protocols
102PROCESSINGProcessing
103EARLY_HINTSEarly Hints
200OKOK
201CREATEDCreated
202ACCEPTEDAccepted
203NON_AUTHORITATIVE_INFORMATIONNon Authoritative Information
204NO_CONTENTNo Content
205RESET_CONTENTReset Content
206PARTIAL_CONTENTPartial Content
207MULTI_STATUSMulti-Status
300MULTIPLE_CHOICESMultiple Choices
301MOVED_PERMANENTLYMoved Permanently
302MOVED_TEMPORARILYMoved Temporarily
303SEE_OTHERSee Other
304NOT_MODIFIEDNot Modified
305USE_PROXYUse Proxy
307TEMPORARY_REDIRECTTemporary Redirect
308PERMANENT_REDIRECTPermanent Redirect
400BAD_REQUESTBad Request
401UNAUTHORIZEDUnauthorized
402PAYMENT_REQUIREDPayment Required
403FORBIDDENForbidden
404NOT_FOUNDNot Found
405METHOD_NOT_ALLOWEDMethod Not Allowed
406NOT_ACCEPTABLENot Acceptable
407PROXY_AUTHENTICATION_REQUIREDProxy Authentication Required
408REQUEST_TIMEOUTRequest Timeout
409CONFLICTConflict
410GONEGone
411LENGTH_REQUIREDLength Required
412PRECONDITION_FAILEDPrecondition Failed
413REQUEST_TOO_LONGRequest Entity Too Large
414REQUEST_URI_TOO_LONGRequest-URI Too Long
415UNSUPPORTED_MEDIA_TYPEUnsupported Media Type
416REQUESTED_RANGE_NOT_SATISFIABLERequested Range Not Satisfiable
417EXPECTATION_FAILEDExpectation Failed
418IM_A_TEAPOTI'm a teapot
419INSUFFICIENT_SPACE_ON_RESOURCEInsufficient Space on Resource
420METHOD_FAILUREMethod Failure
421MISDIRECTED_REQUESTMisdirected Request
422UNPROCESSABLE_ENTITYUnprocessable Entity
423LOCKEDLocked
424FAILED_DEPENDENCYFailed Dependency
426UPGRADE_REQUIREDUpgrade Required
428PRECONDITION_REQUIREDPrecondition Required
429TOO_MANY_REQUESTSToo Many Requests
431REQUEST_HEADER_FIELDS_TOO_LARGERequest Header Fields Too Large
451UNAVAILABLE_FOR_LEGAL_REASONSUnavailable For Legal Reasons
500INTERNAL_SERVER_ERRORInternal Server Error
501NOT_IMPLEMENTEDNot Implemented
502BAD_GATEWAYBad Gateway
503SERVICE_UNAVAILABLEService Unavailable
504GATEWAY_TIMEOUTGateway Timeout
505HTTP_VERSION_NOT_SUPPORTEDHTTP Version Not Supported
507INSUFFICIENT_STORAGEInsufficient Storage
511NETWORK_AUTHENTICATION_REQUIREDNetwork Authentication Required

Migrating from v1.x.x

http-status-codes v2 is mostly backwards compatible with v1. There is a single breaking change and two recommended changes.

[Breaking Change] 'Server Error'

The reason phrase for the status code 500 has been changed from "Server Error" to "Internal Server Error". This is the correct phrase according to RFC7231. If you are migrating from v1, and have code that relies on the result of getStatusText(500) or getReasonPhrase('Server Error'), then this could affect you.

[Non-breaking change] getStatusText renamed getReasonPhrase

The function getStatusText has been renamed to getReasonPhrase. The old function is still available, but may be deprecated in a future version. To fix this simply rename instances of getStatusText() to getReasonPhrase(). The function is otherwise the same as it was before.

[Non-breaking change] StatusCodes

In http-status-codes v1, Status Codes were exported directly from the top-level module. i.e. HttpStatus.OK. In v2 all Status Codes live under an object called StatusCodes. i.e. HttpStatus.StatusCodes.OK. We made this change to cater to TypeScript users who prefer a dedicated value with an enum type. The previous values are still exported, but we won't continue to update them. Please migrate if you're using the old-style imports.

Proposing a new status code

If you'd like to propose a new status code, feel free to update "codes.json" with the necessary information and open a pull request. There is no need to modify source code or even this README. This is done automatically by npm run update-codes.

In general, we try to include only codes that have an official RFC and have been approved, however exceptions can be made if the code is already in widespread use in the wild.

Steps to build and publish

npm run update-codes
npm run test
npm run build
npm version [major | minor | patch]
npm publish

After releasing, please add release notes via GitHub Releases.

Keywords

FAQs

Last updated on 20 Sep 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc