Express.js Mastery / Express: Let’s Start Building The Natours API!

Serving Static Files

πŸ“– Context

"Static files" are files that already exist in the file system (HTML, images, CSS, JavaScript) but currently cannot be accessed via a browser/URL, since no route has been defined for them. This lecture covers how to make these files accessible using a built-in Express middleware.

πŸ’‘ Example Problem

A file like overview.html sits inside a public folder, but visiting a URL like /public/overview.html (or similar) doesn't work β€” because there's no route handler associated with that URL.


πŸ”Ή Step 1: Using express.static in app.js file:

app.use(express.static(`${__dirname}/public`));

πŸ’‘ How each part works:

  • πŸ“ express.static β€” a built-in Express middleware specifically designed to serve static files.

  • πŸ“‚ It takes one argument: the directory to serve files from β€” in this case, the public folder.

  • πŸ“ A template string is used with __dirname to build the correct absolute path, ensuring it works regardless of where the app is run from.

  • βš™οΈ app.use(...) β€” registers this as global middleware, just like any other middleware seen so far.


πŸ”Ή Step 2: Accessing the File β€” And Why the Folder Name Is Dropped from the URL

After adding this middleware, the file can be accessed at:

12.0.0.1:3000/overview.html

❌ Not:

12.0.0.1:3000/public/overview.html

πŸ’‘ Why the public Folder Disappears from the URL

  • πŸ” When Express receives a request for a URL that doesn't match any defined route, it then checks the folder specified in express.static().

  • 🌐 That folder is effectively treated as the root of the site for static file purposes.

  • πŸ“„ So requesting /overview.html causes Express to look for overview.html inside the public folder β€” the public part of the path is implicit, not part of the URL.

πŸ–ΌοΈ This Also Works for Nested Files

For example, an image inside public/img/:

12.0.0.1:3000/img/pin.png

βœ… This correctly serves the image, following the same logic β€” Express looks inside:

public/img/pin.png

Even though public isn't in the URL.


πŸ”Ή Step 3: What Does Not Work

  • ❌ Trying to access a path that isn't an actual static file (i.e., something that looks like a normal route but has no matching file or handler) will not work.

  • πŸ” Express will instead try to match it against defined routes, fail, and return an error, since express.static only kicks in for files that genuinely exist in the specified folder.


πŸ”Ή Step 4: Observing the Browser's Network Requests

  • 🌐 When opening overview.html in the browser, the Developer Tools Network tab shows a separate HTTP request for every asset on the page (images, stylesheets, scripts, etc.).

  • πŸ’‘ This reinforces a core web concept: each resource referenced by an HTML page triggers its own individual request to the server.

πŸ“ In This Demo

  • ⚠️ Most of these asset requests return 404 Not Found, since overview.html wasn't actually designed to be served this way in this project.

  • 🎯 It was only used to demonstrate the static file serving mechanism, not to build a working webpage.


βœ… Key Takeaway

  • πŸ“ express.static() serves static files from a specified folder.

  • 🌐 The folder acts as the root, so its name isn't included in the URL.

  • πŸ“„ It only serves existing static filesβ€”it doesn't create routes.

  • πŸš€ While not essential for building APIs, it's a commonly used Express feature.