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?
const express = require('express'); const app = express(); app.use(express.static('public')); app.listen(3000);
Think about what express.static middleware does with file paths matching the URL.
The express.static middleware serves static files from the specified folder. When the URL matches a file in 'public', it sends that file directly.
Choose the correct way to use express.static middleware to serve files from a folder named 'assets'.
Consider how express.static expects the folder path relative to the project root.
The argument to express.static is a string path relative to the current working directory. 'assets' is correct. Adding slashes or dots can cause errors or unexpected behavior.
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?
Check the URL path and the mount path of the middleware.
When express.static is mounted at '/static', files inside 'public' are served only when the URL starts with '/static'. So /image.png won't work, but /static/image.png will.
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'?
Think about how express.static handles requests for files that are not found.
If the requested file is not found, express.static automatically sends a 404 Not Found response.
Which statement best describes how express.static middleware interacts with other middleware and routes in an Express app?
Consider what happens when express.static finds a matching file to serve.
When express.static finds a file matching the request URL, it sends the file and ends the response. This means no later middleware or routes run for that request.