
When a mobile app shows your messages or a website loads a list of products, it usually asks a server for data through an API (application programming interface). The most common style for web APIs is REST, and Node.js is one of the most popular platforms for building them, because it lets you write the server in JavaScript, the same language used in the browser. This guide explains the principles first and then walks through building a small but complete API for managing a list of books.
REST stands for Representational State Transfer. The term was defined by Roy Fielding in his doctoral dissertation in 2000; Fielding was also one of the principal authors of the HTTP specification. REST is not a library or a standard you install. It is an architectural style, a set of constraints for designing networked applications. The most important ideas for everyday API design are:
Five methods cover nearly every operation:
Every response carries a status code that tells the client what happened. Codes in the 200s mean success: 200 OK, 201 Created after a POST and 204 No Content after a deletion. Codes in the 400s mean the client made a mistake: 400 Bad Request for invalid data, 401 Unauthorized when login is missing, 403 Forbidden when access is not allowed, 404 Not Found for a missing resource. Codes in the 500s mean the server failed. Returning the right code lets clients react correctly without reading error text.
Install Node.js, preferably the current LTS (long-term support) version, from nodejs.org or through a version manager such as nvm. Create a folder, run npm init -y to create a package.json file, and install Express, the most widely used web framework for Node.js, with npm install express. Express adds routing, request parsing and middleware on top of Node's built-in HTTP module. Alternatives such as Fastify, Koa and NestJS follow similar ideas.
In a file called server.js, import Express, create an application with const app = express(), add app.use(express.json()) so the server can read JSON request bodies, and start listening with app.listen(3000). Run it with node server.js. Recent Node.js versions also support node --watch server.js, which restarts the server automatically whenever you save a file.
A route connects a method and a path to a handler function that receives a request object (req) and a response object (res). Start with an array in memory acting as a temporary database, then write one route per operation:
For lists, support query parameters for filtering, sorting and pagination, such as /books?author=Tolstoy&page=2&limit=20. Returning thousands of records in one response is slow for everyone; pagination should be there from the start.
A browser can only send GET requests easily, so use a dedicated tool. curl works from the terminal; graphical clients such as Postman, Insomnia, Bruno or the REST Client extension for VS Code let you build, save and repeat requests. Test the failure cases too: a missing book, an empty body, a wrong data type. Later, write automated tests with a test runner such as Node's built-in node:test, Jest or Vitest, together with Supertest to send requests to the app.
Never trust data from clients. A request may lack required fields, contain the wrong types or be deliberately malicious. Check every incoming body and parameter before using it, and reply with 400 and a clear message explaining what is wrong. Validation libraries such as Zod, Joi or express-validator let you describe the expected shape of the data once, for example a title that is a non-empty string and a year that is an integer, and check it automatically.
Express supports error-handling middleware: a function with four parameters, registered after all routes, that catches errors from any route. Use it to log the full error on the server and return a consistent JSON response to the client, such as an object with an error field. Never send stack traces or database messages to clients in production; they reveal details attackers can use. Since Express 5, errors thrown in asynchronous handlers reach this middleware automatically, which removes a common source of crashed servers.
An in-memory array is lost every time the server restarts. Replace it with a database. PostgreSQL is a popular relational choice, MongoDB a popular document database, and SQLite is ideal for small projects because it stores everything in one file. Libraries such as Prisma, Drizzle or Mongoose make queries easier. Whatever you use, always pass user input as query parameters rather than building SQL strings by joining text, which prevents SQL injection, one of the oldest and most damaging attacks on web applications.
Keep the database address and passwords in environment variables, loaded from a .env file in development, never written in the code.
With these steps you have the core of almost every real-world API: clear resources, correct methods and status codes, validated input, central error handling, persistent storage and basic security. Everything else, from caching to background jobs, builds on this foundation.
Grade 10 Math: Functions and Reasoning - Quiz
1170 questions