Generating Errors using HTTP-errors module in Node.js - GeeksforGeeks

06 Jul, 2021
HTTP-errors module is used for generating errors for Node.js applications. It is very easy to use. We can use it with express, Koa, etc. applications. We will implement this module in an express application.
Installation and Setup: First, initialize the application with package.json file with the following command:  
npm init
 
Then, install the module by the following command:
 
npm install http-errors --save
 
Also, we are using an express application, therefore, install the express module by the following command:
 
npm install express --save
 
Now, create a file and name it app.js. You can name your file whatever you want.
 
For importing the modules in your application, write the following code in your app.js file:
 
javascript
const createError = require('http-errors');const express = require('express');const app = express();
Implementation: Here, comes the main part of our application. For using this module, write the following code in your app.js file: 
javascript
var createError = require('http-errors');var express = require('express');var app = express();  app.use((req, res, next) => {  if (!req.user) return next(    createError(401, 'Login Required!!'));  next();}); app.listen(8080, (err) => {    if (err) console.log(err);    console.log(`Server Running at http://localhost:8080`);});
Here, we are importing the http-errors module and storing it in a variable named as createError. Next, in app.use(), if the user is not authenticated, then our application will create a 401 error saying Login Required!!. The createError is used for generating errors in an application.
To run the code, run the following command in the terminal:
node app.js
and navigate to http://localhost:8080. The output for the above code will be:
List of all Status Code with their Error Message:  
Status
Code    Error Message

400    BadRequest
401    Unauthorized
402    PaymentRequired
403    Forbidden
404    NotFound
405    MethodNotAllowed
406    NotAcceptable
407    ProxyAuthenticationRequired
408    RequestTimeout
409    Conflict
410    Gone
411    LengthRequired
412    PreconditionFailed
413    PayloadTooLarge
414    URITooLong
415    UnsupportedMediaType
416    RangeNotSatisfiable
417    ExpectationFailed
418    ImATeapot
421    MisdirectedRequest
422    UnprocessableEntity
423    Locked
424    FailedDependency
425    UnorderedCollection
426    UpgradeRequired
428    PreconditionRequired
429    TooManyRequests
431    RequestHeaderFieldsTooLarge
451    UnavailableForLegalReasons
500    InternalServerError
501    NotImplemented
502    BadGateway
503    ServiceUnavailable
504    GatewayTimeout
505    HTTPVersionNotSupported
506    VariantAlsoNegotiates
507    InsufficientStorage
508    LoopDetected
509    BandwidthLimitExceeded
510    NotExtended
511    NetworkAuthenticationRequired
Conclusion: The http-errors module is very useful for developers for quick generation of errors with their message. In this article, we learned about the http-errors module for Node.js. We have also seen its installation and Implementation. 
gabaa406
Node.js-Misc
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Express.js express.Router() Function
Node.js fs.readFile() Method
Node.js fs.writeFile() Method
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
Roadmap to Become a Web Developer in 2022
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Convert a string to an integer in JavaScript