Build HTTP APIs with routing, middleware, validation, controllers, and error handling.
Lessons
Use res.sendFile and res.download to serve and stream files.
Sending Files and Downloads is easiest to learn by reading the example, changing it, and observing the result.
import express from 'express';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
app.use(express.static(join(__dirname, 'public')));
app.get('/download', (req, res) => {
res.download(join(__dirname, 'files', 'guide.pdf'));
});
app.listen(3000);Practice the Sending Files and Downloads example in a small scratch file, then explain what changed and why.