0
0
Node.jsframework~10 mins

Serving static files in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Serving static files
Start Server
Receive HTTP Request
Check if Request is for Static File
Yes No
Locate File on Disk
Read File Content
Send File Content as Response
End Request
The server starts and waits for requests. When a request comes, it checks if it's for a static file. If yes, it finds and reads the file, then sends it back as the response.
Execution Sample
Node.js
import express from 'express';
const app = express();
app.use(express.static('public'));
app.listen(3000);
This code creates a server that automatically serves files from the 'public' folder when requested.
Execution Table
StepActionRequest URLFile Path CheckedFile Found?Response Sent
1Server starts and listens----
2Request received/index.htmlpublic/index.htmlYesContents of index.html
3Request received/style.csspublic/style.cssYesContents of style.css
4Request received/image.pngpublic/image.pngNo404 Not Found
5Request received/api/data-NoPass to API handler
💡 Requests stop when response is sent or passed to other handlers.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
Request URL-/index.html/style.css/image.png/api/data
File Path-public/index.htmlpublic/style.csspublic/image.png-
File Found-YesYesNoNo
Response-index.html contentstyle.css content404 Not FoundAPI handler called
Key Moments - 3 Insights
Why does the server send a 404 error for /image.png?
Because the file 'public/image.png' does not exist, as shown in step 4 of the execution_table where File Found? is No.
What happens when the request URL is /api/data?
The server does not find a matching static file and passes the request to another handler, as shown in step 5 where Response Sent is 'Pass to API handler'.
How does the server know which folder to serve static files from?
The folder is specified in the code with express.static('public'), so it looks inside the 'public' folder for requested files.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Response Sent at step 3?
AContents of style.css
B404 Not Found
CContents of index.html
DPass to API handler
💡 Hint
Check the Response Sent column at step 3 in the execution_table.
At which step does the server fail to find the requested file?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the File Found? column in the execution_table to find where it says No.
If the static folder was changed to 'assets', how would the File Path Checked change at step 2?
Apublic/index.html
Bassets/index.html
C/index.html
Dindex.html
💡 Hint
File Path Checked is based on the static folder name plus the request URL.
Concept Snapshot
Serving static files in Node.js with Express:
- Use express.static('folderName') middleware
- Server listens for requests
- If request matches a file in folder, serve it
- If file missing, send 404
- Other requests pass to next handlers
Full Transcript
This visual trace shows how a Node.js server using Express serves static files. The server starts and listens for requests. When a request comes in, it checks if the URL matches a file inside the specified static folder, here 'public'. If the file exists, the server reads and sends its content as the response. If the file does not exist, the server sends a 404 error. Requests that do not match static files are passed to other handlers. The execution table tracks each step, showing the request URL, the file path checked, whether the file was found, and the response sent. Variable tracking shows how request URL, file path, file found status, and response change over time. Key moments clarify why 404 errors happen and how the static folder is set. The quiz tests understanding of these steps and how changing the static folder affects file paths.