0
0
Expressframework~10 mins

Serving from multiple directories in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Serving from multiple directories
Start Express App
Set Static Dir 1
Set Static Dir 2
Client Requests File
Check Dir 1 for File
YesServe File
No
Check Dir 2 for File
YesServe File
No
Send 404 Not Found
Express checks each static directory in order for the requested file and serves it if found; otherwise, it sends a 404 error.
Execution Sample
Express
const express = require('express');
const app = express();
app.use(express.static('public'));
app.use(express.static('assets'));
app.listen(3000);
This code sets up an Express server that serves static files from 'public' first, then 'assets' if not found in 'public'.
Execution Table
StepRequest URLCheck DirectoryFile Found?Action Taken
1/image.pngpublicYesServe /public/image.png
2/style.csspublicNoCheck next directory
3/style.cssassetsYesServe /assets/style.css
4/script.jspublicNoCheck next directory
5/script.jsassetsNoSend 404 Not Found
💡 Request ends when file is found or all directories are checked without success.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
Requested FileNone/image.png/style.css/style.css/script.js/script.js
Current DirectoryNonepublicpublicassetspublicassets
File FoundFalseTrueFalseTrueFalseFalse
Key Moments - 2 Insights
Why does Express check directories in the order they are added?
Express serves the first matching file it finds. The execution_table shows it checks 'public' before 'assets', so files in 'public' take priority.
What happens if the file is not found in any directory?
As shown in step 5 of the execution_table, Express sends a 404 Not Found response when no directories contain the requested file.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what directory is checked for '/style.css' after 'public'?
Aroot
Bpublic
Cassets
Dnode_modules
💡 Hint
Check rows 2 and 3 in the execution_table under 'Check Directory'
At which step does Express send a 404 Not Found response?
AStep 5
BStep 3
CStep 1
DStep 2
💡 Hint
Look at the 'Action Taken' column in the execution_table
If the order of directories is reversed, which directory will Express check first?
Apublic
Bassets
Cnode_modules
Dviews
💡 Hint
The order of app.use(express.static(...)) calls determines the check order
Concept Snapshot
Express serves static files from multiple directories in the order they are added.
Use app.use(express.static('dir')) multiple times.
Requests check each directory in sequence.
First found file is served.
If none found, 404 is sent.
Full Transcript
This example shows how Express serves static files from multiple directories. The server first checks the 'public' directory for the requested file. If the file is found there, it is served immediately. If not, Express checks the next directory, 'assets'. If the file is found in 'assets', it is served. If the file is not found in any directory, Express sends a 404 Not Found response. The order of directories matters because Express stops checking once it finds the file. This behavior allows organizing files in different folders and controlling which files take priority.