,

Handling 404 Error Pages in Express JS | Full Express JS Course in Hindi/Urdu – Episode 9

Posted by

<!DOCTYPE html>

404 Error Page Handles in Express JS

404 Error Page Handles in Express JS

Express JS is a popular web application framework for Node.js that provides powerful features for building web applications. One important aspect of web development is handling error pages, such as the 404 error page.

Handling 404 Error Page in Express JS

When a user requests a page that does not exist on the server, the server returns a 404 error code. In Express JS, we can handle this error and customize the 404 error page to show a user-friendly message to the user.

Creating a Custom 404 Error Page

To create a custom 404 error page in Express JS, we can use the `app.use` method to create a middleware that handles the 404 error.

“`javascript
app.use(function(req, res, next) {
res.status(404).send(“Sorry, the page you are looking for does not exist.”);
});
“`

This code snippet creates a middleware that handles all requests that do not match any route in the application and sends a 404 error message to the user.

Testing the Custom 404 Error Page

To test the custom 404 error page, you can try accessing a non-existent page on your Express JS application. The server should return the custom 404 error message that you have defined.

Conclusion

In this article, we have learned how to handle 404 error pages in Express JS by creating a custom middleware that sends a user-friendly message to the user when a page is not found. Customizing error pages is important for providing a good user experience on your web application.

Express JS Full Course in Hindi/Urdu #9

If you want to learn more about Express JS and web development, check out the Express JS full course in Hindi/Urdu #9 for comprehensive tutorials and examples.

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@sadafangel2030
5 months ago

Well explained, Sir , thnk you 👍

@naghmanrobinson403
5 months ago

Very nice👍