# Building REST APIs with Node.js
A REST API exposes data and actions over HTTP using standard methods — GET, POST, PUT, DELETE — mapped to resources like `/students` or `/courses`.
## A Minimal Server
```javascript
const http = require("http");
const server = http.createServer((req, res) => {
if (req.url === "/courses" && req.method === "GET") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify([{ id: 1, name: "Full Stack" }]));
}
});
server.listen(3000);
```
Raw Node.js works but gets unwieldy fast, which is why frameworks like Express exist.
## Resource-Based Routes
```
GET /courses → list all courses
GET /courses/:id → get one course
POST /courses → create a course
PUT /courses/:id → update a course
DELETE /courses/:id → delete a course
```
Following this convention makes the API predictable for anyone consuming it.
## Status Codes Matter
Returning `200` for everything, including errors, makes an API hard to consume. Use `201` for created resources, `400` for bad input, `404` for missing resources, and `500` for server errors.
## Request Validation
Never trust incoming data. Validate request bodies before touching a database — a missing or malformed field should return a clear `400` error, not crash the server.
## Structuring a Real Project
Separate routes, controllers (business logic), and data access into different layers. This keeps route files thin and makes logic testable independent of HTTP.
## Conclusion
REST API design is mostly about consistency — predictable routes, correct status codes, and clear error responses matter more than clever code.
Back to Blogs
Building REST APIs with Node.js
The fundamentals of building a REST API with Node.js — routes, request handling, status codes, and structuring a real project.
05 Aug 2026
6 min read