📖 Scenario: You are building a simple website using Express.js. Your website needs to show images, styles, and scripts to visitors. These files are called static files because they do not change often. To make your website work well, you need to tell Express where to find these static files.
🎯 Goal: Learn how to serve static files in Express.js by setting up a folder for static content and configuring Express to use it. This will help your website show images, CSS, and JavaScript files correctly.
📋 What You'll Learn
Create a folder named public to hold static files
Set up Express to serve static files from the public folder
Use the express.static middleware correctly
Add a simple route to test the server
💡 Why This Matters
🌍 Real World
Serving static files is essential for websites to show images, styles, and scripts correctly. This setup is common in web projects.
💼 Career
Understanding how to serve static files with Express is a basic skill for backend web developers and helps in building full-stack applications.
Progress0 / 4 steps
1
Create the Express app and import express
Write code to import express and create an app variable by calling express().
Express
Hint
Use require('express') to import Express and then call it to create the app.
2
Create a public folder for static files
Create a folder named public in your project directory to hold static files like images, CSS, and JavaScript. Then, add a variable staticPath and set it to the string 'public'.
Express
Hint
Just create a string variable named staticPath with the value 'public'.
3
Use express.static middleware to serve static files
Use app.use() with express.static(staticPath) to tell Express to serve static files from the public folder.
Express
Hint
Use app.use(express.static(staticPath)) to serve files from the folder.
4
Add a simple route and start the server
Add a route for GET / that sends the text 'Welcome to static files demo'. Then start the server on port 3000 using app.listen.
Express
Hint
Use app.get to create a route and app.listen(3000) to start the server.
Practice
(1/5)
1. Why do we use express.static middleware in an Express app?
easy
A. To handle database connections automatically
B. To serve static files like images, CSS, and JavaScript to the browser
C. To create dynamic routes based on user input
D. To log all incoming requests to the console
Solution
Step 1: Understand the purpose of static files
Static files include images, CSS, and JavaScript that browsers need to display a webpage properly.
Step 2: Role of express.static
This middleware tells Express to serve these files directly from a folder to the browser.
Final Answer:
To serve static files like images, CSS, and JavaScript to the browser -> Option B
Quick Check:
Serving static files = express.static usage [OK]
Hint: Static files are served with express.static middleware [OK]
Common Mistakes:
Confusing static files with dynamic content
Thinking express.static handles database or logging
Assuming express.static creates routes
2. Which of the following is the correct way to serve static files from a folder named public in Express?
easy
A. app.getStatic('public')
B. app.static('public')
C. app.use(express.static('public'))
D. app.serveStatic('public')
Solution
Step 1: Recall Express static middleware syntax
The correct syntax to serve static files is app.use(express.static('folderName')).
Step 2: Check each option
Only app.use(express.static('public')) uses the correct method express.static with app.use.
A. express.static is missing the folder path argument
B. app.listen should come before app.use
C. express.static should be called without parentheses
D. The folder name should be 'public' not 'static'
Solution
Step 1: Check express.static usage
express.static is a function that requires the folder path as an argument.
Step 2: Identify missing argument
The code uses express.static without parentheses and folder name, so it won't work.
Final Answer:
express.static is missing the folder path argument -> Option A
Quick Check:
express.static needs folder path argument [OK]
Hint: express.static must be called with folder path [OK]
Common Mistakes:
Forgetting to call express.static as a function
Thinking app.listen order matters here
Assuming folder name must be 'public'
5. You want to serve static files from two folders: public and uploads. Which code correctly serves both folders so files from either can be accessed by URL?
hard
A. app.use(express.static('public'));
app.use(express.static('uploads'));
B. app.use(express.static(['public', 'uploads']));
Express allows multiple static folders by calling app.use(express.static()) multiple times.
Step 2: Evaluate options
app.use(express.static('public'));
app.use(express.static('uploads')); correctly calls express.static twice for each folder. app.use(express.static(['public', 'uploads'])); is invalid syntax. app.use(express.static('public/uploads')); tries to serve a combined folder that likely doesn't exist. app.static('public');
app.static('uploads'); uses a wrong method.
Final Answer:
app.use(express.static('public'));
app.use(express.static('uploads')); -> Option A