Why is it important to serve static files efficiently in a web app?
Serving static files efficiently improves page load speed and user experience by quickly delivering images, styles, and scripts without extra processing.
Click to reveal answer
Which Express method is used to serve static files from a directory?
Aapp.get()
Bexpress.static()
Capp.listen()
Dexpress.use()
✗ Incorrect
express.static() is the middleware function used to serve static files.
How do you add multiple static directories in Express?
AUse app.static() instead of express.static()
BPass an array of directories to one express.static() call
CServe only one directory at a time
DCall app.use() multiple times with express.static() for each directory
✗ Incorrect
You add multiple static directories by calling app.use() multiple times with express.static() for each folder.
If two static directories have a file named 'logo.png', which one will Express serve?
ABoth files at the same time
BThe file from the directory added last with app.use()
CThe file from the directory added first with app.use()
DExpress will throw an error
✗ Incorrect
Express serves the first matching file it finds based on the order of app.use() calls.
What is a benefit of organizing static files into multiple directories?
ABetter organization and easier maintenance
BSlower server response
CMore complex code with no benefits
DIt disables caching
✗ Incorrect
Using multiple directories helps keep files organized and easier to manage.
Which of these is NOT a static file typically served by Express?
ADatabase queries
BHTML files
CJavaScript files
DCSS files
✗ Incorrect
Database queries are dynamic operations, not static files.
Explain how to serve static files from two different folders in Express and what happens if files have the same name.
Think about middleware order and file lookup.
You got /3 concepts.
Describe why serving static files efficiently matters in a web application and how multiple directories can help.
Consider user experience and code organization.
You got /4 concepts.
Practice
(1/5)
1. What happens when you use multiple app.use(express.static()) calls with different directories in Express?
easy
A. Express throws an error if multiple static directories are used.
B. Express merges all directories into one and serves files randomly from any directory.
C. Express only serves files from the last directory added with express.static().
D. Express serves static files by checking each directory in the order they were added until it finds the file.
Solution
Step 1: Understand how Express static middleware works
Each app.use(express.static()) adds a middleware that checks its directory for requested files.
Step 2: Order of middleware matters
Express checks these middlewares in the order they were added and serves the first matching file it finds.
Final Answer:
Express serves static files by checking each directory in the order they were added until it finds the file. -> Option D
Quick Check:
Multiple static folders checked in order = D [OK]
Hint: Remember: Express checks static folders in order added [OK]
Common Mistakes:
Thinking Express merges directories randomly
Believing only the last directory is used
Assuming multiple static folders cause errors
2. Which of the following is the correct way to serve static files from two directories named public and assets in Express?
easy
A. app.use(express.static('public')); app.use(express.static('assets'));
B. app.use(express.static(['public', 'assets']));
C. app.use(express.static('public', 'assets'));
D. app.use(express.static('public')); app.use.static('assets');
Solution
Step 1: Check Express static syntax
The express.static() middleware accepts only one directory path at a time.
Step 2: Use multiple app.use() calls
To serve multiple directories, call app.use(express.static()) separately for each directory.
Final Answer:
app.use(express.static('public')); app.use(express.static('assets')); -> Option A
Quick Check:
Multiple static folders need separate app.use calls = C [OK]
Hint: Use separate app.use calls for each static folder [OK]
Common Mistakes:
Passing multiple directories in one express.static call
Using incorrect method like app.use.static
Passing an array instead of separate calls
3. Given the code below, what file will Express serve when a user requests /image.png? Assume both directories contain a file named image.png.
A. The first line should use app.get instead of app.use.
B. The second line has a syntax error with missing parentheses and argument.
C. Express cannot serve from more than one directory.
D. The third line should come before the first line.
Solution
Step 1: Check each line for syntax correctness
The second line is app.use(express.static)); which is missing parentheses and directory argument.
Step 2: Understand correct usage of express.static
The express.static() function requires a directory path as an argument and proper parentheses.
Final Answer:
The second line has a syntax error with missing parentheses and argument. -> Option B
Quick Check:
Syntax error in express.static call = B [OK]
Hint: Check parentheses and arguments in express.static calls [OK]
Common Mistakes:
Ignoring missing parentheses or arguments
Thinking app.use is wrong for static files
Believing order of calls causes syntax error
5. You want to serve static files from public and uploads directories, but only files in uploads should be accessible under the URL path /files. How do you configure Express correctly?
hard
A. app.use(express.static('public/uploads'));
app.use(express.static('uploads'));
B. app.use('/files', express.static('public'));
app.use(express.static('uploads'));
C. app.use(express.static('public'));
app.use('/files', express.static('uploads'));
D. app.use(express.static('public'));
app.use(express.static('/files/uploads'));
Solution
Step 1: Serve public directory normally
Use app.use(express.static('public')) to serve files at root URL.
Step 2: Mount uploads directory under /files path
Use app.use('/files', express.static('uploads')) so files in uploads are accessible under /files URL prefix.
Final Answer:
app.use(express.static('public'));
app.use('/files', express.static('uploads')); -> Option C
Quick Check:
Use URL prefix for uploads folder = A [OK]
Hint: Use URL path prefix in app.use for selective static serving [OK]
Common Mistakes:
Serving uploads without URL prefix
Using wrong directory paths
Confusing URL path with filesystem path
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.