codex-lv4-may-2025

Level Navigation: 1 2 3 (4ℹ️) (5ℹ️) 6 7 8 9 10 11 12 13 14⚡ 15⚡ (16ℹ️) 17 18 19 20 21 22 23 24 25 26⚡ 27⚡ 28⚡ 29 30 31 32 33 34 35 36 37 38 39⚡ 40⚡

Level 8: Serve Static Assets

Use app.use(express.static('public')) (or a directory name you prefer) so the server can return HTML, CSS, images, or other assets directly from disk. Seed the directory with a variety of files (styles.css, sample.json, report.csv, logo.png) and hit each one with your browser or Postman.

Show Me: serve static assets

import express from 'express';

const app = express();

// Serve static files from the 'public' directory
// Files in public/ will be accessible at the root URL
// e.g., public/styles.css → http://localhost:3000/styles.css
app.use(express.static('public'));

const port = 3000;
app.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});

Directory structure example:

your-project/
├── src/
│   └── index.js
├── public/
│   ├── styles.css
│   ├── sample.json
│   ├── report.csv
│   └── logo.png
└── package.json

Testing: