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?
const express = require('express'); const app = express(); app.use(express.static('public')); app.use(express.static('assets')); app.listen(3000);
Think about the order in which middleware is applied in Express.
Express checks middleware in the order they are added. Since express.static('public') is added first, it serves files from 'public' before checking 'assets'. So the 'public' version of logo.png is served.
Choose the correct Express code snippet that serves static files from both 'public' and 'uploads' directories.
Remember how app.use accepts middleware functions.
Each call to app.use adds one middleware. Option B correctly adds two separate static middlewares. Options A and B are invalid because express.static expects a string, not multiple arguments or arrays. Option B passes an array, which app.use does not accept as middleware.
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?
const express = require('express'); const app = express(); app.use(express.static('public')); app.use('/uploads', express.static('uploads')); app.listen(3000);
Check how the URL path relates to the mount path in app.use.
The second static middleware is mounted at '/uploads', so files inside 'uploads' directory are served only when the URL starts with '/uploads'. Requesting '/uploads/image.png' is correct. Requesting '/image.png' looks only in 'public'. If '/uploads/image.png' returns 404, likely the file is missing or the URL is incorrect.
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?
Think about middleware order and how Express processes requests.
Express processes middleware in the order they are added. The first static middleware that finds the requested file serves it. It does not merge files or throw errors for duplicates.
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?
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);
Consider how Express moves through middleware and when it stops.
Request passes Middleware 1, then tries to serve from 'public' but file not found, so next middleware runs (Middleware 2), then 'assets' static serves the file. Middleware 2 runs once. So console logs 'Middleware 1' then 'Middleware 2'.