Challenge - 5 Problems
Static Files Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when you use express.static middleware?
Consider this Express code snippet:
What will happen when a user visits
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);
Attempts:
2 left
💡 Hint
Think about what express.static middleware does with files in the folder you specify.
✗ Incorrect
express.static middleware serves files from the specified folder directly when the URL matches a file path inside it. So if image.png is inside public, it will be sent to the user.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about what browsers need to show a webpage correctly.
✗ Incorrect
Static files like images, CSS, and JavaScript are essential for the browser to render the page with styles, interactivity, and visuals. Serving them properly ensures the user sees the page as intended.
📝 Syntax
advanced2: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'?
Attempts:
2 left
💡 Hint
Check the Express documentation for the static middleware usage.
✗ Incorrect
The correct syntax is app.use(express.static('folderName')). Option A uses this correctly. Option A tries to use app.get which is incorrect for static middleware. Options A and C have syntax errors.
🔧 Debug
advanced2:30remaining
Why does this static file request fail?
Given this Express code:
What URL should a user visit to access
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?Attempts:
2 left
💡 Hint
Look at the path prefix used in app.use for static files.
✗ Incorrect
The static middleware is mounted at /static, so files inside 'public' are served under /static. Visiting /style.css does not match the static middleware and returns 404.
❓ state_output
expert3:00remaining
What is the output when serving static files with cache control?
Consider this Express code:
If a user requests
app.use(express.static('public', { maxAge: '1d' }));If a user requests
/logo.png, what HTTP header related to caching will the server send?Attempts:
2 left
💡 Hint
maxAge sets how long browsers can cache the file in seconds.
✗ Incorrect
Setting maxAge to '1d' tells Express to send Cache-Control header with max-age of 86400 seconds (1 day), allowing browsers to cache the file for that time.