Introduction
While surfing the internet, we all have encountered an error page saying -
"Error 404. Page not Found".
Have you ever pondered what this means?
When you search a URL, for example, codingninjas.com, a request is sent to the server responsible for serving the traffic directed towards this domain name. The request asks the server to respond with the page associated with this URL.
Many times, there exists no page/data corresponding to the requested URL. This case may arise when the requested URL is invalid.
In such situations, the server responds with an error page with status code 404, implying no data available.
We have the option of designing the error page the way we want. This article focuses on adding a 404 error page in websites using Express.js.
Express.js is a robust node.js framework. One of the framework's primary advantages is defining distinct routes or middleware to handle the client's various incoming requests.
In this tutorial, we'll use the express server to create a 404 error page.
Before you move ahead, make sure you have node.js and npm installed in your system. If not, please follow this guide to set up node.js - https://www.codingninjas.com/studio/library/setting-up-node-js-on-windows
Install the express dependency
Create a directory learn_express and navigate to this directory in command prompt or terminal in Linux.
Run the following command to get started -
npm init |
To install express as a dependency for your project, run the following command -
npm install express |
After running this command, the directory structure should look this -