02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Creating a middleware in ExpressJS

Creating a Middleware in Express.js

In the last lecture, we covered the concept of middleware in Express.js, and now we're ready to take it a step deeper by learning how to create our own middleware. Middleware is a powerful tool that allows you to add custom functionality to your server, such as error handling, request processing, or adding common behaviors to all routes. In this tutorial, we will explore how to create our own custom middleware that handles requests in a versatile way.

What is Middleware?

Middleware functions in Express.js are pieces of code that run between the request and response lifecycle, allowing you to modify requests, responses, or add custom logic before the final response is sent back to the client. Middleware can be used to handle errors, log requests, validate data, or even modify response headers.

Creating a Custom Middleware

Let's dive right in and create our own middleware function that handles "404 - Page Not Found" errors. Typically, when a user requests a URL that doesn't exist, you want to let them know with a custom error page or message. Express.js doesn't handle 404 errors by default, as it's an unopinionated framework that gives developers flexibility to define their own rules. That's where middleware comes into play.

Step 1: Setting Up the Middleware

The middleware function we're going to create will catch any request that wasn't handled by our route definitions. To do that, we will add our middleware to the very bottom of the route definitions, ensuring it only runs after all other routes have been evaluated.

Here's an example of what the middleware might look like:

// Custom 404 Middleware
app.use((req, res, next) => {
  // Set the status code to 404, indicating page not found
  res.status(404);
  // Send an error message to the client
  res.send('Sorry, I cannot find this page');
});

Step 2: Understanding the Middleware Structure

In the code above, the function has three parameters: req, res, and next.

  • req: The request object that contains information about the client's request, such as URL, headers, and body.
  • res: The response object that allows you to send back a response to the client.
  • next: A callback function that allows you to pass control to the next middleware in the stack, if any.

Since this middleware is designed to handle errors (specifically, "404 - Not Found" errors), there is no next() call in this example. Once the middleware is reached, it sets the status to 404 and sends a custom error message back to the client.

Step 3: Using Middleware in Express.js

Middleware can be placed anywhere in the request-response lifecycle, and their order is important. Here, we add our custom middleware at the very end, making sure it runs after all the defined routes:

// Other route handlers\...

// Custom 404 Middleware must be placed at the end\app.use((req, res) => {
  res.status(404).send('Sorry, I cannot find this page');
});

Advanced Middleware Concepts

Middlewares can be much more powerful when you need them to be. For example, you can use middleware to perform logging, check user authentication, or even add custom headers. You can also use the next() function to pass control to the next middleware if multiple middlewares need to run for the same request.

In this example, the next callback is used when middleware is not the endpoint of a request and other handlers need to be executed afterward. This approach is beneficial if you need to create reusable functions to enhance your application.

app.use((req, res, next) => {
  console.log('Time:', Date.now());
  next();
});

Practical Usage

In our middleware example, we created a basic "404 - Not Found" handler. You could expand this handler to include more informative responses, such as a redirect to a help page or logging the error to a database.

The next() function becomes particularly useful when chaining multiple middleware functions. For example, you could use it to log requests before passing them on to your route handlers.

// Log request information middleware
app.use((req, res, next) => {
  console.log(`Request URL: ${req.url}`);
  console.log(`Request Method: ${req.method}`);
  next(); // Pass control to the next middleware or route handler
});

In a real-world application, you could add custom error pages, user-friendly messages, or even dynamic redirects based on user roles or permissions.

Updates Since 2016

Note: Since this video was created in 2016, some best practices around middleware have evolved.

  • Deprecation of Body Parser: In Express version 4.16.0+, body-parser has been added as part of the Express module itself. Instead of installing and using body-parser, you can now use express.json() and express.urlencoded().
  • Arrow Functions: This example uses modern JavaScript arrow functions, which may not have been common in 2016 but are widely adopted today for cleaner and more concise syntax.

Summary

In this tutorial, we learned how to create a custom middleware in Express.js to handle 404 errors. Middleware is a powerful tool that allows us to extend the functionality of our application, whether it's handling errors, logging, or other utilities. In the next lesson, we will explore automating the process of creating applications using the Express generator.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!

Starting up an Express.js server

Learn how to start up an Express.js server, including installation, basic setup, and creating a Hello World server.

08:08

Enabling static files

Learn how to enable static files in Express.js to serve HTML, CSS, JavaScript, and other assets in your web application.

04:47

Routing with Express

In this video, explore the basics of routing in Express.js and how to create dynamic content. Learn about GET, POST, PUT, DELETE, and how to handle different routes effectively.

09:08

Routing with Forms and Static Files in Express.js

Learn how to create dynamic routes with forms and static files using Express.js. This is essential for building interactive web applications.

06:14

Using Middleware in Express.js

Learn how to integrate third-party middleware in Express.js, specifically using body-parser, to handle form data and extend server functionality.

07:29

Creating a middleware in ExpressJS

Learn how to create your own middleware in ExpressJS to handle custom server-side logic and responses.

03:54

Using Express Generator

Learn how to quickly scaffold Express.js applications using the Express Generator tool. This tool helps automate server setup and structure, making development faster and easier.

04:34

Back to React

Learn about integrating Webpack and Babel with React to streamline full stack development.

18:16