Section 1: Node.js and Express basics
How a Node.js server works
Node.js is a JavaScript runtime built on the V8 engine. It lets JavaScript run outside the browser, with access to the file system, the network and operating system features. For servers, its key feature is non blocking I/O: while one request waits for the database, Node handles other requests on the same thread using the event loop.
This makes Node efficient for APIs that spend most of their time waiting on databases and other services. It is less suited to heavy CPU work, like video encoding, inside request handlers, because that blocks every other request until it finishes.
What a web server does
At its core, an HTTP server:
- Listens on a port for incoming requests.
- Reads the method, URL, headers and body.
- Decides what code should handle it, called routing.
- Sends back a status code, headers and a body.
Node's built in http module can do all of this, but you would write a lot of repetitive code for parsing bodies, matching routes and handling errors. Express is a small framework that adds routing and a middleware system on top, without hiding how HTTP works.
Why Express 5
Express has been around for a long time, which means a huge ecosystem of middleware and answers to almost every question online. Express 5, released as the stable major version after a long development period, modernises it:
- Errors thrown in async handlers and rejected promises are passed to your error handler automatically. In Express 4, an unhandled rejection in a route could leave the request hanging.
- Route path matching was updated and made stricter, with some older syntax removed.
- Support for very old Node.js versions was dropped.
If you maintain Express 4 apps, a later lesson covers the main migration points.
Modern Node features we will use
node --watchrestarts the server when files change, without extra tools.node --env-file=.envloads environment variables from a file.- The built in test runner,
node --test, and thenode:assertmodule. - Native
fetchfor calling other APIs.
These remove several dependencies that older tutorials still install.
Recipebox
Our API stores recipes for users. A recipe has a title, ingredients, steps, tags and a visibility setting. Users can make collections public and share a link. Simple, but full of real decisions about validation, permissions and data modelling.