APIs with Express · Chapter 10 of 29

Static files

By the end of this chapter you can serve a folder of files from Express: under any prefix, with the right cache headers, falling through to your routes, and without serving secrets. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Middleware
  • 5 tasks
  • Express
  • In the all labs plan

The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.

Why you need this

Anjali has finished the Tapri website: an index.html, a stylesheet, a page of monsoon offers and a folder of menu photos. She wants the API to serve them, with no second server for a handful of files. Kabir adds two conditions: phones should keep the big photos instead of downloading them on every visit, and the .env file someone copied into the folder must never go out.

With what you know, every file would need its own route that reads it, picks a Content-Type and sends it. A new photo would mean a new route, and nobody would answer the browser's "has this changed?" question, so every visit downloads everything again.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    publicDir: the website at the root

    In createApp({ publicDir, photosDir }), serve the files in publicDir at the site root. GET / answers 200 with publicDir's index.html as text/html, GET /menu.css answers 200 with menu.css as text/css, and GET /offers/ answers 200 with offers/index.html.

  2. Task 2

    photosDir: menu photos under /photos

    Serve the files in photosDir under the prefix /photos: GET /photos/masala-chai.jpg answers 200 with the photo's exact bytes and Content-Type: image/jpeg.

    The prefix is not part of the file path, and the photos are not at the root: GET /masala-chai.jpg and GET /photos/photos/masala-chai.jpg both answer 404.

  3. Task 3

    Photos cached for a week, revalidated with ETag

    Photos go out with exactly Cache-Control: public, max-age=604800, immutable (a week, and a promise that the file never changes), plus an ETag and a Last-Modified header. A request that sends that ETag back in If-None-Match answers 304 with an empty body.

    The site's own files keep the default: GET /menu.css has Cache-Control: public, max-age=0.

  4. Task 4

    JSON 404s for missing files

    A path with no file falls through to the API: GET /api/menu answers 200 with the menu. Anything that matches no file and no route answers 404 with { error: 'Not found' }, for example GET /nope.css.

    A missing photo answers 404 with { error: 'Photo not found' } instead: GET /photos/filter-coffee.jpg.

  5. Task 5 · interview question

    Interview: /.well-known served, .env hidden

    Dotfiles stay hidden: GET /.env, /%2Eenv, /.git/config and /photos/.notes all answer 404, and no answer contains any of their contents.

    But GET /.well-known/tapri.json answers 200 with that file from publicDir/.well-known, as JSON: { partnerApi: '/api' }.

What you will learn

  • express.static: a middleware that answers URL paths with files from a folder, index pages included.
  • A prefix: the same middleware mounted under a path.
  • Cache headers: Cache-Control, ETag and the 304 that saves a download.
  • Fallthrough and dotfiles: what happens when no file matches, and which files are never served.

More in Middleware

Build "Static files" yourself

Partner cafés want Tapri’s menu online by Friday: you build its API. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.