0
0
Expressframework~20 mins

express.static middleware - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Static Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does express.static middleware do in this code?

Consider this Express.js 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 assuming public/image.png exists?

Express
const express = require('express');
const app = express();
app.use(express.static('public'));
app.listen(3000);
AThe server will return a 404 error because no route is defined for '/image.png'.
BThe server will send the file 'public/image.png' to the user.
CThe server will redirect the user to '/public/image.png'.
DThe server will respond with a JSON listing of files in the 'public' folder.
Attempts:
2 left
💡 Hint

Think about what express.static middleware does with file paths matching the URL.

📝 Syntax
intermediate
1:30remaining
Which option correctly uses express.static to serve files from 'assets' folder?

Choose the correct way to use express.static middleware to serve files from a folder named 'assets'.

Aapp.use(express.static('assets'));
Bapp.use(express.static('/assets'));
Capp.use(express.static('./assets'));
Dapp.use(express.static('assets/'));
Attempts:
2 left
💡 Hint

Consider how express.static expects the folder path relative to the project root.

🔧 Debug
advanced
2:30remaining
Why does this express.static middleware not serve files as expected?

Look at this code:

const express = require('express');
const app = express();
app.use('/static', express.static('public'));
app.listen(3000);

When visiting http://localhost:3000/image.png, the file does not load. Why?

ABecause the URL must start with '/static' to serve files from 'public'.
BBecause the folder 'public' does not exist.
CBecause the server is not listening on port 3000.
DBecause express.static cannot be used with a path prefix.
Attempts:
2 left
💡 Hint

Check the URL path and the mount path of the middleware.

state_output
advanced
2:00remaining
What is the output when accessing a missing file with express.static?

Given this Express app:

const express = require('express');
const app = express();
app.use(express.static('public'));
app.listen(3000);

What happens when a user requests http://localhost:3000/missing.txt if missing.txt does not exist in 'public'?

AThe server redirects to the root URL '/'.
BThe server responds with a 500 Internal Server Error.
CThe server responds with an empty 200 OK response.
DThe server responds with a 404 Not Found error.
Attempts:
2 left
💡 Hint

Think about how express.static handles requests for files that are not found.

🧠 Conceptual
expert
3:00remaining
How does express.static middleware affect the request lifecycle in Express?

Which statement best describes how express.static middleware interacts with other middleware and routes in an Express app?

Aexpress.static modifies the request object to include file metadata but does not send responses.
Bexpress.static only works if placed after all route handlers in the middleware stack.
Cexpress.static handles matching requests and ends the response, preventing later middleware or routes from running for those requests.
Dexpress.static always passes control to the next middleware regardless of whether it serves a file.
Attempts:
2 left
💡 Hint

Consider what happens when express.static finds a matching file to serve.