Introduction
express.static middleware helps your Express app serve files like images, CSS, and JavaScript easily to users without extra code.
app.use(express.static('folderName'))app.use(express.static('public'))app.use('/static', express.static('assets'))
app.use('/files', express.static(path.join(__dirname, 'files')))
import express from 'express'; import path from 'path'; const app = express(); const PORT = 3000; // Serve static files from 'public' folder app.use(express.static('public')); app.get('/', (req, res) => { res.send('<h1>Welcome!</h1><img src="/logo.png" alt="Logo">'); }); app.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); });
express.static middleware in an Express app?express.staticexpress.static middleware serves these files directly from a folder, making them accessible via URLs.express.static to serve files from a folder named public?app.use() with the middleware function inside.express.static function takes the folder name as a string and returns middleware, so app.use(express.static('public')) is correct.app.use('/assets', express.static('public'));
// Client requests: GET /assets/image.pngapp.use(express.static('public'));
app.use('/static', express.static('public'));/static/style.css, what happens?public at root URL and uploads at /files. Which code correctly sets this up?app.use(express.static('public')) serves files from 'public' at root URL.app.use('/files', express.static('uploads')) serves files from 'uploads' folder under '/files' URL.Pass the test and this pattern moves unknown