Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Install the express dependency
3.
Create a server to handle request
4.
Output
5.
Install the express dependency
6.
Create a server to handle request
7.
Output
8.
Key Takeaways
Last Updated: Mar 27, 2024

How to add error 404 in Dynamic website using express

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 - 

 

 

Create a server to handle request

Create a file index.js in this directory and write the following code in it -

const express = require("express"// module integration.
const app = express() // this function returns an object.
  
// Handling GET /home request.
app.get("/home", (req, res, next) => {
    res.send("<h1>This is the home page.</h1>");
});

// Handling GET /about requests.
app.get("/about", (req, res, next) => {
    res.send("<h1>This is the about page.</h1>");
});

// Handling GET /contact request.
app.get("/contact", (req, res, next) => {
    res.send("<h1>This is the contact page.</h1>");
});

// Handling requests that do not match the above cases.
app.use((req, res, next) => {
    res.status(404).send(
        "<h1>Page not found on the server</h1>")
});
  
// Starting the server.
app.listen(3000, () => {
    console.log("Server is Running")
});

 

Run the JS file using the following command - 

node index.js

 

Output

Now visit localhost:3000/home. You should see an output like this.

 

However, if you visit localhost:3000, you will see something like this. 

It is because our application does not handle this URI endpoint.

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 - 

 

Create a server to handle request

Create a file index.js in this directory and write the following code in it -

const express = require("express"// module integration.
const app = express() // this function returns an object.
  
// Handling GET /home request.
app.get("/home", (req, res, next) => {
    res.send("<h1>This is the home page.</h1>");
});

// Handling GET /about requests.
app.get("/about", (req, res, next) => {
    res.send("<h1>This is the about page.</h1>");
});

// Handling GET /contact request.
app.get("/contact", (req, res, next) => {
    res.send("<h1>This is the contact page.</h1>");
});

// Handling requests that do not match the above cases.
app.use((req, res, next) => {
    res.status(404).send(
        "<h1>Page not found on the server</h1>")
});
  
// Starting the server.
app.listen(3000, () => {
    console.log("Server is Running")
});

 

Run the JS file using the following command - 

node index.js

 

Output

Now visit localhost:3000/home. You should see an output like this.

However, if you visit localhost:3000, you will see something like this. 

It is because our application does not handle this URI endpoint.

Key Takeaways

In this blog, we learned how to add a 404 error page in Dynamic Website using Express JS. We also saw how to install Express JS as a dependency in a project. Express JS is a robust NodeJS framework that helps in creating server-side web applications faster and smarter.

Check out this problem - Smallest Distinct Window .

Live masterclass