Express.js & REST APIs
Node.js REST API Design
A REST API models resources as URLs and uses HTTP methods for actions. Consistent nouns, correct status codes and predictable response shapes are what make an API pleasant to consume.
What is REST API Design in Node.js?
A REST API models resources as URLs and uses HTTP methods for actions. Consistent nouns, correct status codes and predictable response shapes are what make an API pleasant to consume.
REST API Design example
JavaScript
res.json({
data: jobs,
meta: { page, limit, total, totalPages: Math.ceil(total / limit) },
});Key points to remember
- Use plural nouns for collections and never verbs in paths.
- Version the API — /api/v1 — before you have external consumers.
- Always paginate list endpoints; an unbounded list will eventually break.
- Return the same error shape everywhere so clients can handle it once.
Conventional resource routes
| Method | Path | Purpose | Success status |
|---|---|---|---|
| GET | /api/jobs | list, with filters and pagination | 200 |
| GET | /api/jobs/:id | fetch one | 200 or 404 |
| POST | /api/jobs | create | 201 with Location |
| PUT | /api/jobs/:id | replace entirely | 200 |
| PATCH | /api/jobs/:id | update some fields | 200 |
| DELETE | /api/jobs/:id | remove | 204 |
Common mistakes with REST API Design
- Returning 200 with an error message in the body — clients cannot detect failure.
- Endpoints named /getJobs or /createJob, which duplicate the HTTP method.
Node.js REST API Design— Interview Questions & FAQs
What is the difference between PUT and PATCH?+
PUT replaces the whole resource with the payload, so omitted fields are cleared. PATCH applies a partial update, changing only the fields you send.
