Bird
Raised Fist0
Expressframework~20 mins

Why serving static files matters in Express - Challenge Your Understanding

Choose your learning style10 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.

Practice

(1/5)
1. Why do we use express.static middleware in an Express app?
easy
A. To handle database connections automatically
B. To serve static files like images, CSS, and JavaScript to the browser
C. To create dynamic routes based on user input
D. To log all incoming requests to the console

Solution

  1. Step 1: Understand the purpose of static files

    Static files include images, CSS, and JavaScript that browsers need to display a webpage properly.
  2. Step 2: Role of express.static

    This middleware tells Express to serve these files directly from a folder to the browser.
  3. Final Answer:

    To serve static files like images, CSS, and JavaScript to the browser -> Option B
  4. Quick Check:

    Serving static files = express.static usage [OK]
Hint: Static files are served with express.static middleware [OK]
Common Mistakes:
  • Confusing static files with dynamic content
  • Thinking express.static handles database or logging
  • Assuming express.static creates routes
2. Which of the following is the correct way to serve static files from a folder named public in Express?
easy
A. app.getStatic('public')
B. app.static('public')
C. app.use(express.static('public'))
D. app.serveStatic('public')

Solution

  1. Step 1: Recall Express static middleware syntax

    The correct syntax to serve static files is app.use(express.static('folderName')).
  2. Step 2: Check each option

    Only app.use(express.static('public')) uses the correct method express.static with app.use.
  3. Final Answer:

    app.use(express.static('public')) -> Option C
  4. Quick Check:

    Correct syntax = app.use(express.static('folder')) [OK]
Hint: Use app.use with express.static('folder') to serve files [OK]
Common Mistakes:
  • Using app.static instead of express.static
  • Forgetting to use app.use middleware
  • Using non-existent methods like getStatic or serveStatic
3. What will happen if you run this Express code?
const express = require('express');
const app = express();
app.use(express.static('assets'));
app.listen(3000);

And you visit http://localhost:3000/image.png where assets/image.png exists?
medium
A. The server will return a 404 Not Found error
B. The browser will download a blank page
C. The server will crash with an error
D. The image.png file will be served and displayed in the browser

Solution

  1. Step 1: Understand express.static behavior

    express.static serves files from the specified folder matching the URL path.
  2. Step 2: Match URL to file path

    Visiting /image.png looks for assets/image.png. Since it exists, Express serves it.
  3. Final Answer:

    The image.png file will be served and displayed in the browser -> Option D
  4. Quick Check:

    File exists + express.static = file served [OK]
Hint: URL path matches static folder files to serve them [OK]
Common Mistakes:
  • Assuming 404 if static middleware is used
  • Thinking server crashes on static file requests
  • Confusing static folder name with URL path
4. Identify the error in this Express code snippet that tries to serve static files from static folder:
const express = require('express');
const app = express();
app.use(express.static);
app.listen(3000);
medium
A. express.static is missing the folder path argument
B. app.listen should come before app.use
C. express.static should be called without parentheses
D. The folder name should be 'public' not 'static'

Solution

  1. Step 1: Check express.static usage

    express.static is a function that requires the folder path as an argument.
  2. Step 2: Identify missing argument

    The code uses express.static without parentheses and folder name, so it won't work.
  3. Final Answer:

    express.static is missing the folder path argument -> Option A
  4. Quick Check:

    express.static needs folder path argument [OK]
Hint: express.static must be called with folder path [OK]
Common Mistakes:
  • Forgetting to call express.static as a function
  • Thinking app.listen order matters here
  • Assuming folder name must be 'public'
5. You want to serve static files from two folders: public and uploads. Which code correctly serves both folders so files from either can be accessed by URL?
hard
A. app.use(express.static('public')); app.use(express.static('uploads'));
B. app.use(express.static(['public', 'uploads']));
C. app.use(express.static('public/uploads'));
D. app.static('public'); app.static('uploads');

Solution

  1. Step 1: Understand serving multiple static folders

    Express allows multiple static folders by calling app.use(express.static()) multiple times.
  2. Step 2: Evaluate options

    app.use(express.static('public')); app.use(express.static('uploads')); correctly calls express.static twice for each folder. app.use(express.static(['public', 'uploads'])); is invalid syntax. app.use(express.static('public/uploads')); tries to serve a combined folder that likely doesn't exist. app.static('public'); app.static('uploads'); uses a wrong method.
  3. Final Answer:

    app.use(express.static('public')); app.use(express.static('uploads')); -> Option A
  4. Quick Check:

    Multiple app.use(express.static()) calls serve multiple folders [OK]
Hint: Call app.use(express.static()) for each folder separately [OK]
Common Mistakes:
  • Trying to pass an array to express.static
  • Combining folder names incorrectly
  • Using non-existent app.static method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard