Using Built-in Middleware for Static Files

Tutorial 3 of 5

Introduction

This tutorial aims to provide a comprehensive guide on how to use built-in middleware in Express.js to serve static files such as HTML, CSS, and JavaScript. Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle.

By the end of this tutorial, you will learn how to:

  • Understand the concept of middleware in Express.js
  • Use Express.js built-in middleware for serving static files
  • Implement middleware for static files in your own Express.js application

To get the most out of this tutorial, you should have a basic understanding of JavaScript, Node.js, and Express.js.

Step-by-Step Guide

What is Middleware?

Middleware functions are those that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. They can execute any code, make changes to the request and the response objects, end the request-response cycle, and call the next middleware in the stack.

Serving Static Files with Express.js Middleware

By default, Express.js does not serve static files like HTML, CSS, and JavaScript. To serve static files, use the built-in express.static middleware function in Express.

Here is an example:

var express = require('express');
var app = express();

app.use(express.static('public'));

In the above code, express.static('public') is a built-in middleware function in Express. It serves static files and takes the name of the folder as an argument, from where the static files are to be served (in this case 'public').

Code Examples

Here's an example to serve static files with Express.js middleware:

var express = require('express');
var app = express();

// Serve static files from the 'public' directory
app.use(express.static('public'));

app.listen(3000, function() {
  console.log('Express server is listening on port 3000');
});

In this code snippet, we are using the built-in middleware function express.static to serve static files. This middleware function is called every time the app receives a request. It checks if the request is for a file in the 'public' directory, and if so, it sends that file as the response.

Summary

In this tutorial, we discussed how to use middleware in Express.js to serve static files. We learned that Express.js does not serve static files by default, and we can use the built-in express.static middleware function to serve static files.

To continue learning about Express.js and middleware, you can read the official Express.js documentation, which provides more detailed information and examples.

Practice Exercises

  1. Create an Express.js application that serves static files from a 'public' directory. Test your application by placing an HTML file in the 'public' directory and accessing it in your web browser.

  2. Modify your application to serve static files from a different directory, such as 'assets'. Test your application again to ensure it is serving files from the new directory.

  3. Add a route handler for a specific path (e.g., '/special') and make it serve static files from a unique directory (e.g., 'special-files'). Test your application to ensure it serves files from the unique directory when you access the specific path.

Remember to practice regularly and experiment with different scenarios to get the most out of Express.js middleware for serving static files. Good luck!