Bird
Raised Fist0
Expressframework~20 mins

Serving from multiple directories in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Static Serving Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when serving static files from two directories?

Consider this Express setup serving static files from two directories:

const express = require('express');
const app = express();

app.use(express.static('public'));
app.use(express.static('assets'));

app.listen(3000);

If both public and assets folders contain a file named logo.png, which file will be served when requesting /logo.png?

Express
const express = require('express');
const app = express();

app.use(express.static('public'));
app.use(express.static('assets'));

app.listen(3000);
AThe logo.png file from the 'public' directory is served.
BThe logo.png file from the 'assets' directory is served.
CAn error occurs because of duplicate files.
DThe server randomly picks one of the two files to serve.
Attempts:
2 left
💡 Hint

Think about the order in which middleware is applied in Express.

📝 Syntax
intermediate
2:00remaining
Which code correctly serves static files from multiple directories?

Choose the correct Express code snippet that serves static files from both 'public' and 'uploads' directories.

Aapp.use(express.static('public', 'uploads'));
B
app.use(express.static('public'));
app.use(express.static('uploads'));
Capp.use([express.static('public'), express.static('uploads')]);
Dapp.use(express.static(['public', 'uploads']));
Attempts:
2 left
💡 Hint

Remember how app.use accepts middleware functions.

🔧 Debug
advanced
2:00remaining
Why does this Express app fail to serve files from the second directory?

Look at this Express code:

const express = require('express');
const app = express();

app.use(express.static('public'));
app.use('/uploads', express.static('uploads'));

app.listen(3000);

Requesting /uploads/image.png returns 404, but /image.png from 'public' works. Why?

Express
const express = require('express');
const app = express();

app.use(express.static('public'));
app.use('/uploads', express.static('uploads'));

app.listen(3000);
AThe static middleware for 'uploads' is incorrectly mounted and should be before 'public'.
BThe 'uploads' directory does not exist, causing 404 errors.
CThe URL path must include '/uploads' prefix to access files in 'uploads' directory.
DExpress cannot serve static files from multiple directories with different URL prefixes.
Attempts:
2 left
💡 Hint

Check how the URL path relates to the mount path in app.use.

🧠 Conceptual
advanced
2:00remaining
How does Express handle overlapping files in multiple static directories?

If two static middlewares serve files from different directories and both contain a file named index.html, which file will Express serve when requesting /index.html?

AExpress serves the file from the last static middleware added.
BExpress merges the two files and serves a combined version.
CExpress throws an error due to file name conflict.
DExpress serves the file from the first static middleware added that contains the file.
Attempts:
2 left
💡 Hint

Think about middleware order and how Express processes requests.

❓ state_output
expert
2:00remaining
What is the console output after these requests?

Given this Express app:

const express = require('express');
const app = express();

app.use((req, res, next) => {
  console.log('Middleware 1');
  next();
});

app.use(express.static('public'));

app.use((req, res, next) => {
  console.log('Middleware 2');
  next();
});

app.use(express.static('assets'));

app.listen(3000);

If a request is made for /style.css which exists only in the 'assets' directory, what will be printed in the console?

Express
const express = require('express');
const app = express();

app.use((req, res, next) => {
  console.log('Middleware 1');
  next();
});

app.use(express.static('public'));

app.use((req, res, next) => {
  console.log('Middleware 2');
  next();
});

app.use(express.static('assets'));

app.listen(3000);
A
Middleware 1
Middleware 2
BMiddleware 1
C
Middleware 1
Middleware 2
Middleware 2
D
Middleware 1
Middleware 2
Middleware 1
Attempts:
2 left
💡 Hint

Consider how Express moves through middleware and when it stops.

Practice

(1/5)
1. What happens when you use multiple app.use(express.static()) calls with different directories in Express?
easy
A. Express throws an error if multiple static directories are used.
B. Express merges all directories into one and serves files randomly from any directory.
C. Express only serves files from the last directory added with express.static().
D. Express serves static files by checking each directory in the order they were added until it finds the file.

Solution

  1. Step 1: Understand how Express static middleware works

    Each app.use(express.static()) adds a middleware that checks its directory for requested files.
  2. Step 2: Order of middleware matters

    Express checks these middlewares in the order they were added and serves the first matching file it finds.
  3. Final Answer:

    Express serves static files by checking each directory in the order they were added until it finds the file. -> Option D
  4. Quick Check:

    Multiple static folders checked in order = D [OK]
Hint: Remember: Express checks static folders in order added [OK]
Common Mistakes:
  • Thinking Express merges directories randomly
  • Believing only the last directory is used
  • Assuming multiple static folders cause errors
2. Which of the following is the correct way to serve static files from two directories named public and assets in Express?
easy
A. app.use(express.static('public')); app.use(express.static('assets'));
B. app.use(express.static(['public', 'assets']));
C. app.use(express.static('public', 'assets'));
D. app.use(express.static('public')); app.use.static('assets');

Solution

  1. Step 1: Check Express static syntax

    The express.static() middleware accepts only one directory path at a time.
  2. Step 2: Use multiple app.use() calls

    To serve multiple directories, call app.use(express.static()) separately for each directory.
  3. Final Answer:

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

    Multiple static folders need separate app.use calls = C [OK]
Hint: Use separate app.use calls for each static folder [OK]
Common Mistakes:
  • Passing multiple directories in one express.static call
  • Using incorrect method like app.use.static
  • Passing an array instead of separate calls
3. Given the code below, what file will Express serve when a user requests /image.png? Assume both directories contain a file named image.png.
app.use(express.static('public'));
app.use(express.static('assets'));
medium
A. The image.png from the public directory.
B. The image.png from the assets directory.
C. An error because the file exists in both directories.
D. No file will be served; Express will return 404.

Solution

  1. Step 1: Understand middleware order

    Express checks static directories in the order they are added. Here, public is checked first.
  2. Step 2: First match is served

    Since image.png exists in public, Express serves that file and stops checking further.
  3. Final Answer:

    The image.png from the public directory. -> Option A
  4. Quick Check:

    First matching static file served = A [OK]
Hint: Express serves first found file in order of static folders [OK]
Common Mistakes:
  • Assuming Express merges files from both directories
  • Thinking Express throws error on duplicates
  • Believing the last directory has priority
4. Identify the error in the following Express code that tries to serve static files from two directories:
app.use(express.static('public'));
app.use(express.static));
app.use(express.static('assets'));
medium
A. The first line should use app.get instead of app.use.
B. The second line has a syntax error with missing parentheses and argument.
C. Express cannot serve from more than one directory.
D. The third line should come before the first line.

Solution

  1. Step 1: Check each line for syntax correctness

    The second line is app.use(express.static)); which is missing parentheses and directory argument.
  2. Step 2: Understand correct usage of express.static

    The express.static() function requires a directory path as an argument and proper parentheses.
  3. Final Answer:

    The second line has a syntax error with missing parentheses and argument. -> Option B
  4. Quick Check:

    Syntax error in express.static call = B [OK]
Hint: Check parentheses and arguments in express.static calls [OK]
Common Mistakes:
  • Ignoring missing parentheses or arguments
  • Thinking app.use is wrong for static files
  • Believing order of calls causes syntax error
5. You want to serve static files from public and uploads directories, but only files in uploads should be accessible under the URL path /files. How do you configure Express correctly?
hard
A. app.use(express.static('public/uploads')); app.use(express.static('uploads'));
B. app.use('/files', express.static('public')); app.use(express.static('uploads'));
C. app.use(express.static('public')); app.use('/files', express.static('uploads'));
D. app.use(express.static('public')); app.use(express.static('/files/uploads'));

Solution

  1. Step 1: Serve public directory normally

    Use app.use(express.static('public')) to serve files at root URL.
  2. Step 2: Mount uploads directory under /files path

    Use app.use('/files', express.static('uploads')) so files in uploads are accessible under /files URL prefix.
  3. Final Answer:

    app.use(express.static('public')); app.use('/files', express.static('uploads')); -> Option C
  4. Quick Check:

    Use URL prefix for uploads folder = A [OK]
Hint: Use URL path prefix in app.use for selective static serving [OK]
Common Mistakes:
  • Serving uploads without URL prefix
  • Using wrong directory paths
  • Confusing URL path with filesystem path
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