0
0
Expressframework~20 mins

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

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