0
0
Node.jsframework~20 mins

Serving static files in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Static File Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when accessing a static file?
Given the following Node.js Express server code, what will be the HTTP status code when requesting /images/logo.png if the file exists in the public/images folder?
Node.js
import express from 'express';
const app = express();
app.use(express.static('public'));
app.listen(3000);
A301
B404
C500
D200
Attempts:
2 left
💡 Hint
Static middleware serves files from the folder specified.
component_behavior
intermediate
2:00remaining
How does express.static handle directory requests?
If a user requests /docs/ and the public/docs/index.html file exists, what will express.static serve?
Node.js
app.use(express.static('public'));
AIt lists all files inside <code>public/docs</code> as a directory listing.
BIt returns a 404 error because directories are not served.
CIt serves the <code>index.html</code> file inside <code>public/docs</code>.
DIt redirects to <code>/docs/index.html</code> URL.
Attempts:
2 left
💡 Hint
Express static middleware automatically serves index.html files in directories.
📝 Syntax
advanced
2:00remaining
Which option correctly sets a custom static folder with a URL prefix?
You want to serve static files from the assets folder but only when the URL starts with /static. Which code snippet is correct?
Aapp.use('/static', express.static('assets'));
Bapp.use(express.static('/static', 'assets'));
Capp.use('/assets', express.static('static'));
Dapp.use(express.static('assets', '/static'));
Attempts:
2 left
💡 Hint
The first argument to app.use is the URL prefix, the second is the middleware.
🔧 Debug
advanced
2:00remaining
Why does this static file request return 404?
Consider this code:
import express from 'express';
const app = express();
app.use(express.static('./public'));
app.listen(3000);
Requesting /style.css returns 404, but the file public/style.css exists. What is the likely cause?
Node.js
import express from 'express';
const app = express();
app.use(express.static('./public'));
app.listen(3000);
AThe file permissions on style.css prevent it from being served.
BThe relative path './public' is incorrect; it should be an absolute path or no './'.
CExpress static middleware does not serve CSS files by default.
DThe server is not listening on port 3000.
Attempts:
2 left
💡 Hint
Relative paths can cause issues depending on where the server is started.
🧠 Conceptual
expert
3:00remaining
What happens if multiple static middleware serve the same URL path?
If you have these two lines in your Express app:
app.use(express.static('public'));
app.use(express.static('assets'));
And both folders contain a file named logo.png, which file will be served when requesting /logo.png?
Node.js
app.use(express.static('public'));
app.use(express.static('assets'));
AThe file from the 'public' folder will be served because middleware are checked in order.
BThe file from the 'assets' folder will be served because it is the last middleware.
CExpress will merge both files and serve a combined response.
DA 500 error will occur due to conflicting static files.
Attempts:
2 left
💡 Hint
Middleware order matters in Express.