Sometimes you want to serve files like images, styles, or scripts from different folders in your web app. Serving from multiple directories lets you organize files better and share them easily.
Serving from multiple directories in Express
app.use(express.static('folder1')) app.use(express.static('folder2'))
app.use(express.static('public')) app.use(express.static('assets'))
app.use('/images', express.static('img')) app.use('/css', express.static('styles'))
This Express app serves static files from two folders: 'public' and 'shared'. Files in 'public' are checked first, then 'shared'.
For example, if you have 'public/index.html' and 'shared/logo.png', both can be accessed via the browser.
import express from 'express' const app = express() const port = 3000 // Serve static files from 'public' folder app.use(express.static('public')) // Also serve static files from 'shared' folder app.use(express.static('shared')) app.listen(port, () => { console.log(`Server running at http://localhost:${port}`) })
Order matters: Express serves files from the first matching folder it finds.
You can use URL prefixes to separate folders, like app.use('/media', express.static('media'))
Make sure folder names and file names do not conflict to avoid confusion.
Serving from multiple directories helps organize static files better.
Use multiple app.use(express.static()) calls to add folders.
Express checks folders in order and serves the first matching file.
Practice
app.use(express.static()) calls with different directories in Express?Solution
Step 1: Understand how Express static middleware works
Eachapp.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 DQuick Check:
Multiple static folders checked in order = D [OK]
- Thinking Express merges directories randomly
- Believing only the last directory is used
- Assuming multiple static folders cause errors
public and assets in Express?Solution
Step 1: Check Express static syntax
Theexpress.static()middleware accepts only one directory path at a time.Step 2: Use multiple
To serve multiple directories, callapp.use()callsapp.use(express.static())separately for each directory.Final Answer:
app.use(express.static('public')); app.use(express.static('assets')); -> Option AQuick Check:
Multiple static folders need separate app.use calls = C [OK]
- Passing multiple directories in one express.static call
- Using incorrect method like app.use.static
- Passing an array instead of separate calls
/image.png? Assume both directories contain a file named image.png.app.use(express.static('public'));
app.use(express.static('assets'));Solution
Step 1: Understand middleware order
Express checks static directories in the order they are added. Here,publicis checked first.Step 2: First match is served
Sinceimage.pngexists inpublic, Express serves that file and stops checking further.Final Answer:
The image.png from the public directory. -> Option AQuick Check:
First matching static file served = A [OK]
- Assuming Express merges files from both directories
- Thinking Express throws error on duplicates
- Believing the last directory has priority
app.use(express.static('public'));
app.use(express.static));
app.use(express.static('assets'));Solution
Step 1: Check each line for syntax correctness
The second line isapp.use(express.static));which is missing parentheses and directory argument.Step 2: Understand correct usage of express.static
Theexpress.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 BQuick Check:
Syntax error in express.static call = B [OK]
- Ignoring missing parentheses or arguments
- Thinking app.use is wrong for static files
- Believing order of calls causes syntax error
public and uploads directories, but only files in uploads should be accessible under the URL path /files. How do you configure Express correctly?Solution
Step 1: Serve public directory normally
Useapp.use(express.static('public'))to serve files at root URL.Step 2: Mount uploads directory under /files path
Useapp.use('/files', express.static('uploads'))so files in uploads are accessible under/filesURL prefix.Final Answer:
app.use(express.static('public')); app.use('/files', express.static('uploads')); -> Option CQuick Check:
Use URL prefix for uploads folder = A [OK]
- Serving uploads without URL prefix
- Using wrong directory paths
- Confusing URL path with filesystem path
Ready to prove it?
Pass the test and this pattern moves unknown
