Understand clients, servers and APIs
A client initiates a request; a server accepts requests and provides responses. An API is an interface with an agreed contract. In a web API, that contract commonly describes request methods, paths, response status codes and data. Node.js can implement either side of such communication.
Local practice: The proposed teaching runtime is Node.js 26.13.1 LTS. These original offline examples were executed on Node.js 22.16.0 (Linux x64); execution on the proposed baseline and in ByteMind is not yet verified. No npm dependencies are required. Use a fresh practice directory. Code blocks are static, not a browser-based Node runner.
Model a catalog response without networking
const request = { path: "/books/7" };const response = { status: 200, title: "River Maps" };console.log(request.path);console.log(JSON.stringify(response));In the directory containing the files above, run:
node main.mjsExpected standard output:
/books/7{"status":200,"title":"River Maps"}Expected exit status: 0.
How it works
A request might ask for the price of a book. A response reports the result, possibly with an HTTP status code. Status 200 means a successful request; 404 means the target resource was not found. The response body carries data. The following programs model a request/response decision locally; they are not real HTTP servers. Objects group named fields, if chooses a branch, and JSON.stringify() converts a value to JSON text for display. Real network requests and transport are covered later.
Model a missing catalog item
const requestedId = 99;const availableId = 7;if (requestedId === availableId) { console.log(200);} else { console.log(404);}In the directory containing the files above, run:
node main.mjsExpected standard output:
404Expected exit status: 0.
Common mistake
Printing a status code is not sending an HTTP response. A real HTTP server must listen on a socket, parse requests and write response messages. The sample deliberately separates application decisions from networking; it cannot be accessed through a browser URL.
Try it yourself
Draw a client requesting book 7, a server looking it up, and the server returning a response. Change requestedId to 7 and predict the result. Explain which part of the drawing is missing from this local model.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Understand clients, servers and APIs — knowledge check
Check your understanding of understand clients, servers and apis.
Who initiates a typical HTTP request?
What is the second program actually doing?
What does 404 normally indicate in HTTP?