0
0
Expressframework~20 mins

Why serving static files matters in Express - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Static Files Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when you use express.static middleware?
Consider this Express code snippet:
const express = require('express');
const app = express();
app.use(express.static('public'));
app.listen(3000);

What will happen when a user visits http://localhost:3000/image.png if public/image.png exists?
Express
const express = require('express');
const app = express();
app.use(express.static('public'));
app.listen(3000);
AThe server will send a JSON response with the file name.
BThe server will return a 404 error because no route is defined for /image.png.
CThe server will redirect the user to /public/image.png automatically.
DThe server will send the image.png file from the public folder to the user.
Attempts:
2 left
💡 Hint
Think about what express.static middleware does with files in the folder you specify.
🧠 Conceptual
intermediate
1:30remaining
Why serve static files in a web app?
Why is it important to serve static files like images, CSS, and JavaScript separately in an Express app?
ABecause static files contain server secrets and must be hidden.
BBecause static files are needed by browsers to display and style pages properly.
CBecause static files slow down the server and should be avoided.
DBecause static files are only used for backend logic.
Attempts:
2 left
💡 Hint
Think about what browsers need to show a webpage correctly.
📝 Syntax
advanced
2:00remaining
Identify the correct way to serve static files in Express
Which of the following code snippets correctly serves static files from a folder named 'assets'?
Aapp.use(express.static('assets'));
Bapp.get('/assets', express.static('assets'));
Capp.use('/assets', express.static);
Dapp.static('assets');
Attempts:
2 left
💡 Hint
Check the Express documentation for the static middleware usage.
🔧 Debug
advanced
2:30remaining
Why does this static file request fail?
Given this Express code:
app.use('/static', express.static('public'));

What URL should a user visit to access public/style.css? What happens if they visit /style.css instead?
AVisit /style.css to get the file; visiting /static/style.css returns 404.
BBoth /style.css and /static/style.css serve the file.
CVisit /static/style.css to get the file; visiting /style.css returns 404.
DNeither URL serves the file; static files are disabled.
Attempts:
2 left
💡 Hint
Look at the path prefix used in app.use for static files.
state_output
expert
3:00remaining
What is the output when serving static files with cache control?
Consider this Express code:
app.use(express.static('public', { maxAge: '1d' }));

If a user requests /logo.png, what HTTP header related to caching will the server send?
ACache-Control: public, max-age=86400
BCache-Control: no-cache
CExpires: 0
DContent-Type: image/png
Attempts:
2 left
💡 Hint
maxAge sets how long browsers can cache the file in seconds.