ExpressJs Error Handling and Custom Error Pages
ExpressJs is a popular web framework for Node.js that allows developers to easily build web applications and APIs. One important aspect of any web application is error handling. In this guide, we will explore how to handle errors in ExpressJs and how to create custom error pages.
Syntax
ExpressJs provides a built-in error handling middleware that can be used to handle errors in an application. The middleware function has the following syntax:
app.use(function (err, req, res, next) {
// Handle error here
})
err
: The error object thrown by the application.req
: The request object.res
: The response object.next
: The next middleware function.
Example
Here is an example of how to handle errors in an ExpressJs application:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
throw new Error('Something went wrong')
})
// Error handling middleware
app.use(function (err, req, res, next) {
console.error(err.stack)
res.status(500).send('Internal Server Error')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
In this example, we throw an error when the user visits the root URL. The error handling middleware logs the error to the console and sends a 500 error status code and message to the user.
Output
When running the above ExpressJs application, if we visit the root URL, the output will be:
$ node app.js
Example app listening at http://localhost:3000
Error: Something went wrong
at app.get (/Users/user/expressjs/app.js:7:9)
at Layer.handle [as handle_request] (/Users/user/expressjs/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/user/expressjs/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/user/expressjs/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/user/expressjs/node_modules/express/lib/router/layer.js:95:5)
at /Users/user/expressjs/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/user/expressjs/node_modules/express/lib/router/index.js:335:12)
at next (/Users/user/expressjs/node_modules/express/lib/router/index.js:275:10)
at expressInit (/Users/user/expressjs/node_modules/express/lib/middleware/init.js:40:5)
at Layer.handle [as handle_request] (/Users/user/expressjs/node_modules/express/lib/router/layer.js:95:5)
Explanation
When an error is thrown in an ExpressJs application, it will be caught by the error handling middleware and passed to the middleware function. This function can then handle the error in any way it sees fit.
In the example above, we simply log the error to the console and send a 500 error status code and message to the user. However, you could also add additional logic to send an email to the developer or display a specific error message to the user.
Use
Error handling is an important aspect of any web application. By handling errors properly, you can improve the user experience and prevent data loss or security vulnerabilities.
Important Points
- ExpressJs provides a built-in error handling middleware to handle errors in an application.
- The middleware function has the syntax
app.use(function (err, req, res, next) {...})
- The error object, request object, response object, and next middleware function are passed to the middleware function.
- The middleware function can handle errors in any way it sees fit, such as logging the error, sending an email, or displaying a specific error message to the user.
Summary
In this guide, we have learned how to handle errors in an ExpressJs application using the built-in error handling middleware. We have also seen an example of how to log errors to the console and send a 500 error status code and message to the user. By properly handling errors, we can improve the user experience and prevent data loss or security vulnerabilities.