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

Enabling static files

Enabling Static Files in Express.js

In this tutorial, we will learn how to serve static files using Express.js. Static files include HTML, CSS, JavaScript, images, and other assets that don't change dynamically. By serving static files, we can enhance the performance of our web applications and provide users with more immersive content, such as images and style sheets.

Step 1: Setting Up Static File Serving

In the last video, we created a basic Express.js server. Now, we will enhance that server to serve static files from a specific directory in our project. To do this, we need to make use of the express.static() middleware.

Open your index.js file and add the following line of code to set up the public directory as a source of static files:

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

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

This line of code tells Express to serve any file placed in the public directory directly to the user. This means that any HTML, CSS, images, or JavaScript placed in the public folder will now be accessible via a URL.

Update (2024): If you're working with ES Modules, consider importing Express like this: import express from 'express';. Also, make sure your package.json file has "type": "module" specified.

Step 2: Creating a Public Folder

The next step is to create a public folder in your project directory. Inside this folder, you can add static files such as an HTML file, CSS files, JavaScript files, and images.

For example, let's create a simple index.html file inside the public folder with the following content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Static Site</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>Welcome to My Static Site!</h1>
    <p>This is an example of serving static files using Express.js.</p>
</body>
</html>

You can add a styles.css file to the public folder as well to style the HTML content.

Step 3: Restarting the Server

After adding the static files, you need to restart your server to see the changes. Stop the server using CTRL + C (or Command + C on macOS) and start it again using the following command:

node index.js

Once the server is running, open your browser and navigate to http://localhost:3000. You should see the content from index.html that you added to the public folder.

Testing the Static File Access

To test if the static file serving is working, you can add an image or another HTML file to the public folder and access it directly via the browser. For example, if you add an image named logo.png inside the public folder, you can access it by navigating to http://localhost:3000/logo.png.

This makes it really easy to serve assets that are required by your front end without writing any additional server-side code.

Conclusion

In this tutorial, you learned how to enable static file serving in Express.js by using the express.static() middleware. Serving static files is a crucial part of creating user-friendly and visually appealing web applications. In the next lesson, we'll take a look at how to handle routing and create more dynamic server responses using Express.js.

Stay tuned, and keep building!

Video Transcript

00:01In the last lecture, we created a basic express JS server
00:05using node js and express.
00:07In this lecture we're gonna take it a step further
00:09by enabling static files.
00:11So by the end of this lecture we'll be able to serve up
00:14to users O2 simple HTML images
00:18and so forth to the user directly instead
00:21of always having dynamic things on top
00:23of the dynamic nature of the language.
00:25We're gonna do that using the used method.
00:28And really this is a hint to middlewares, which we're,
00:31we'll take focus on what a middleware is in a couple
00:34of lectures, but remember the word middleware,
00:36which is relates very closely to
00:38what we're gonna be doing right now.
00:41So let's jump right into it.
00:42Let's see how we enable static files.
00:45Our server is still running from the last video,
00:48but in a real world scenario, we really would want
00:50to have static files as well.
00:52So in between the videos I placed inside
00:55of our express JS folder into the public folder,
00:59a very simple HTML file,
01:01the same one we used in a previous chapter.
01:04My goal right now is to turn all the stuff inside
01:06of the public folder to be publicly accessible.
01:10To do that, I'm gonna go back into my editor
01:13and into my index dot js
01:15and what I want to do is I want to, even
01:17before I set anything else down here, I'm gonna go ahead
01:21and, you know, I'm gonna un um, comment,
01:24I'm gonna comment out this just for a second here
01:27'cause I don't want to add this, uh, get,
01:29we're gonna look more into that in the next lecture.
01:31But what I want to do, and I'm gonna do here two steps.
01:34I want to, first of all, I wanna break out the actual
01:37express because there's other methods there
01:39and other things that I want to gain access to.
01:43What I wanna do is I want to go into my, I want
01:45to basically c create that cons, I'm gonna call it express
01:49and I just want to fetch the actual that require express
01:53and move it into a separate line so I could then refer back
01:56to that express method
01:58because now I'm actually gonna break it apart
02:00and not only have an app,
02:01but I also wanna approach directly express to ask from it
02:05for some functionality.
02:07Now what I wanna do is I wanna basically go to my app
02:11and I want to use express static
02:16and I'm gonna set whatever the folder path that I want, that
02:20static folder path to be accessible.
02:23In this case, I've created the folder that is called public.
02:26And if I go ahead to and call your public and just send that
02:29and just click here on to save, that's all I really had
02:33to do to enable and make it accessible.
02:36Now anything living inside
02:38of the public folder will be then accessible to the web
02:42on my server as a static file,
02:44as static files more accurately.
02:47So lemme go into my terminal
02:48and let's stop our server right now.
02:50So I'm going to uh, type control C
02:53or on a mackey would be
02:56command CI think it was command, yeah, control
02:59C actually. Alright,
03:00so we stopped our server and I want to go ahead
03:02and restart it again.
03:03And as I'm restarting it, I'm gonna go back onto my server
03:07and I'm gonna search for it in my local host in port 3006.
03:11So local host
03:143006 and here we go.
03:17Our public file that is sitting inside there
03:19that is called index html, is now publicly accessible.
03:23Let me just go ahead here and just duplicate this file
03:26and I'm just gonna call it two just to show you
03:28that I could have access to all the, all the files
03:31that are living inside of here.
03:32And let just go ahead here and I'm just gonna go into my app
03:36and I'll just add your into the body
03:38that it'll have a style.
03:40Let's put here a style, um, background red,
03:45lemme go ahead and save it.
03:47And if I go back into my application, I don't need
03:49to restart my server
03:50because we're talking about static files
03:52that are running really on the client side.
03:55So I'm just gonna go ahead here
03:56and check if I could get access to that HTML FI file
03:59and surely I do.
04:01Now this works with any files, includes images,
04:04JavaScript files, any file that you want to just send over
04:07to the client without touching it on the server.
04:10So that's it, that's how you integrate static files.
04:13But really the story of working on the server goes
04:16beyond just static files, which is
04:18what we're gonna look into in the next lecture when we
04:21explore routes or route.
04:24In this lecture we enabled static files on our express JS
04:28node JS server and the next lecture we're gonna take it a
04:32step further because what we're gonna do next is not only
04:34have static files served up,
04:37but we're also gonna learn how to route
04:38with express moving into the world of Dynamicness.
04:42That is really the heart of server side languages
04:45and servers.

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!