First Server

Installing Express and Your First Server

Express 24,430 needs Node 18 or later (engines: >= 18); this book uses Node 24 LTS.

Starting an Express projectShell
mkdir bookshelf && cd bookshelf
npm init -y
npm pkg set type=module
npm install express          # added 68 packages in 1s

Sixty-eight packages and 3.5 MB on disk is the whole framework. It stays small because Express delegates to focused packages from the jshttp and expressjs organizations — router for matching, body-parser for bodies, serve-static for files, qs for query strings. The type=module line lets you write import instead of require (type and File Extensions).

Put the What Express Adds listing in server.js and run node server.js. It prints listening on http://localhost:3000 and stays up; in another terminal:

Output of 2
$ curl -s localhost:3000/books/1
{"id":1,"title":"Node in Practice","authorId":7}
$ curl -s localhost:3000/books/99
{"error":"not found"}
$ curl -s localhost:3000/nope
<!DOCTYPE html> ... <pre>Cannot GET /nope</pre> ...

The last two responses are the trap. A missing book gets your handler's JSON 404; a missing route gets Express's built-in 404, which is HTML, and a client calling response.json() on it breaks. Mounting and 404s fixes that with a catch-all.