đ Introduction
In web applications, static files are files that are delivered to the client exactly as they are stored on the server. These files include HTML pages, CSS stylesheets, JavaScript files, images, videos, fonts, and other assets that do not require server-side processing. Express.js provides the built-in express.static() middleware to serve static files efficiently.
Information
đ Prerequisites
- Node.js installed.
- Express.js installed.
- A basic Express server already created.
- Basic understanding of folders and files.
đ What Are Static Files?
Static files are resources that remain unchanged until they are manually updated. Unlike dynamic content, these files are sent directly to the client without additional processing.
| File Type | Examples |
|---|---|
| HTML | index.html, about.html |
| CSS | style.css |
| JavaScript | app.js, script.js |
| Images | logo.png, banner.jpg |
| Fonts | Roboto.ttf |
| Videos | intro.mp4 |
đ Project Structure
đ Serving a Static Folder
Use the express.static() middleware to serve all files inside the public folder.
index.js
const express = require('express');
const app = express();
// Serve static files from the "public" folder
app.use(express.static('public'));
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});Success
đ Accessing Static Files
| File | URL |
|---|---|
| public/index.html | http://localhost:3000/index.html |
| public/style.css | http://localhost:3000/style.css |
| public/script.js | http://localhost:3000/script.js |
| public/images/logo.png | http://localhost:3000/images/logo.png |
đ Example Static Files
public/index.html
<!DOCTYPE html>
<html>
<head>
<title>Express Static Files</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Welcome to Express.js</h1>
<button onclick="showMessage()">Click Me</button>
<script src="script.js"></script>
</body>
</html>public/style.css
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
text-align: center;
}
h1 {
color: #007acc;
}public/script.js
function showMessage() {
alert('Hello from Express.js!');
}đ Serving Multiple Static Directories
Express allows multiple static folders by registering the middleware multiple times.
Multiple Static Folders
app.use(express.static('public'));
app.use(express.static('assets'));Express searches the registered directories in the order they are defined and serves the first matching file.
đ Using a Virtual Path Prefix
You can expose a folder under a custom URL prefix using a virtual path.
Virtual Path Prefix
app.use('/static', express.static('public'));With this configuration:
| File | Accessible URL |
|---|---|
| public/style.css | /static/style.css |
| public/images/logo.png | /static/images/logo.png |
đ Request Flow for Static Files
đ Middleware Execution Process
đĄ Best Practices
- Store all static assets inside a dedicated folder such as public.
- Organize images, CSS, JavaScript, and fonts into separate subfolders.
- Use meaningful file names for easier maintenance.
- Use virtual path prefixes when serving multiple asset directories.
- Avoid placing sensitive files inside publicly accessible directories.
- Keep application logic separate from static assets.
Warning
đ Learn More
Explore the official Express.js documentation for serving static assets:
âĸ Serving Static Files
âĸ Express.js Official Documentation