HTTP Response Status Codes

HTTP Response Status Codes

Overview

Building APIs have never been easy without a library that gives you all the HTTP status codes in a box. That's why I put together an elegant, flexible, and abstracted npm with all HTTP API status response codes as defined by Request for Comments (RFC).

Usage


const express = require('express');
const { Response } = require('http-status-codez');
const User = require('./models/user');

const app = express();

app.get('/api/v1/users', catchAsync(async (req, res, next) => {
  const users = await User.find();

  res.status(Response.HTTP_OK).json({
    status: 'success',
    results: users.length,
    data: {
      users,
    },
  });

}));

app.get('/api/v1/users/:id', catchAsync(async (req, res, next) => {
  const user = await User.findById(req.params.id);

  if (!user) {
    return next(
      new AppError('User not found with that ID', Response.HTTP_NOT_FOUND);
    );
  }

  res.status(Response.HTTP_OK).json({
    status: 'success',
    data: {
      user,
    },
  });

Implementation Description

CodeInstance PropertiesPhrase
100HTTP_CONTINUEContinue
101HTTP_SWITCHING_PROTOCOLSSwitching Protocols
102HTTP_PROCESSINGProcessing
103HTTP_EARLY_HINTSEarly Hints
200HTTP_OKOK
201HTTP_CREATEDCreated
202HTTP_ACCEPTEDAccepted
203HTTP_NON_AUTHORITATIVE_INFORMATIONNon Authoritative Information
204HTTP_NO_CONTENTNo Content
205HTTP_RESET_CONTENTReset Content
206HTTP_PARTIAL_CONTENTPartial Content
207HTTP_MULTI_STATUSMulti-Status
208HTTP_ALREADY_REPORTEDAlready Reported
226HTTP_IM_USEDIM Used
300HTTP_MULTIPLE_CHOICESMultiple Choices
301HTTP_MOVED_PERMANENTLYMoved Permanently
302HTTP_FOUNDFound
303HTTP_SEE_OTHERSee Other
304HTTP_NOT_MODIFIEDNot Modified
305HTTP_USE_PROXYUse Proxy
306HTTP_RESERVEDReserved
307HTTP_TEMPORARY_REDIRECTTemporary Redirect
308HTTP_PERMANENTLY_REDIRECTPermanent Redirect
400HTTP_BAD_REQUESTBad Request
401HTTP_UNAUTHORIZEDUnauthorized
402HTTP_PAYMENT_REQUIREDPayment Required
403HTTP_FORBIDDENForbidden
404HTTP_NOT_FOUNDNot Found
405HTTP_METHOD_NOT_ALLOWEDMethod Not Allowed
406HTTP_NOT_ACCEPTABLENot Acceptable
407HTTP_PROXY_AUTHENTICATION_REQUIREDProxy Authentication Required
408HTTP_REQUEST_TIMEOUTRequest Timeout
409HTTP_CONFLICTConflict
410HTTP_GONEGone
411HTTP_LENGTH_REQUIRELength Required
412HTTP_PRECONDITION_FAILEDPrecondition Failed
413HTTP_REQUEST_ENTITY_TOO_LARGERequest Entity Too Large
414HTTP_REQUEST_URI_TOO_LONGRequest-URI Too Long
415HTTP_UNSUPPORTED_MEDIA_TYPEUnsupported Media Type
416HTTP_REQUESTED_RANGE_NOT_SATISFIABLERequested Range Not Satisfiable
417HTTP_EXPECTATION_FAILEDExpectation Failed
418HTTP_I_AM_A_TEAPOTI'm a teapot
419HTTP_INSUFFICIENT_SPACE_ON_RESOURCEInsufficient Space on Resource
420HTTP_METHOD_FAILUREMethod Failure
421HTTP_MISDIRECTED_REQUESTMisdirected Request
422HTTP_UNPROCESSABLE_ENTITYUnprocessable Entity
423HTTP_LOCKEDLocked
424HTTP_FAILED_DEPENDENCYFailed Dependency
425HTTP_TOO_EARLYToo Early
426HTTP_UPGRADE_REQUIREDUpgrade Required
428HTTP_PRECONDITION_REQUIREDPrecondition Required
429HTTP_TOO_MANY_REQUESTSToo Many Requests
431HTTP_REQUEST_HEADER_FIELDS_TOO_LARGERequest Header Fields Too Large
451HTTP_UNAVAILABLE_FOR_LEGAL_REASONSUnavailable For Legal Reasons
500HTTP_INTERNAL_SERVER_ERRORInternal Server Error
501HTTP_NOT_IMPLEMENTEDNot Implemented
502HTTP_BAD_GATEWAYBad Gateway
503HTTP_SERVICE_UNAVAILABLEService Unavailable
504HTTP_GATEWAY_TIMEOUTGateway Timeout
505HTTP_VERSION_NOT_SUPPORTEDHTTP Version Not Supported
506HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTALVariant Also Negotiates
507HTTP_INSUFFICIENT_STORAGEInsufficient Storage
508HTTP_LOOP_DETECTEDLoop Detecte
5010HTTP_NOT_EXTENDEDNot Extended
511HTTP_NETWORK_AUTHENTICATION_REQUIREDNetwork Authentication Required