REST API Basics
REST (Representational State Transfer) is an architectural style for designing networked applications. It relies on a stateless, client-server, cacheable communications protocol — and in virtually all cases, the HTTP protocol is used.
REST is not a protocol or a standard but a set of architectural constraints. API developers can implement REST in a variety of ways.
Core Principles
For an API to be considered RESTful, it typically conforms to the following constraints:
1. Client-Server Architecture
The client (frontend) and server (backend) act independently. The client should know how to fetch data, and the server should know how to serve it. This separation allows for portability and scalability.
2. Statelessness
The server does not store any state about the client session on the server side. Each request from the client to server must contain all of the information necessary to understand the request, and cannot take advantage of any stored context on the server.
3. Cacheable
Clients can cache responses. Responses must define themselves as cacheable or not to prevent clients from reusing stale or inappropriate data in response to further requests.
4. Resource-Based URLs (Nouns, Not Verbs)
URLs should represent resources, not actions. Use nouns instead of verbs.
- Bad:
,❌ - Good:
,✅
5. Proper Use of HTTP Methods
Always use HTTP methods semantically to improve clarity and consistency. Each HTTP method has a clear meaning:
6. Consistent Response Structure
Responses should follow a consistent format. Consistency makes APIs easier to consume and debug.
Example:
7. Versioning
Always version your API to handle breaking changes gracefully.
8. Code on Demand (Optional)
Servers can temporarily extend or customize the functionality of a client by the transfer of executable code (e.g., scripts).
9. Filtering, Sorting, and Pagination (Optional)
Prevent large data dumps by implementing query parameters.
Express Implementation Example:
HTTP Methods
In a REST API, endpoints (URLs) represent resources, and HTTP verbs (methods) represent the actions performed on those resources.
Here is how you implement them in Node.js with Express:
GET
Retrieve a representation of a resource. This should not modify the server state.
POST
Create a new resource. The payload is sent in the request body.
PUT
Update or Replace a resource entirely. If the resource doesn't exist, it can be created (depending on implementation).
PATCH
Partially Update a resource. Only the fields sent in the body are updated.
DELETE
Remove a resource.
HTTP Status Codes
Using the correct status code is crucial for a REST API to communicate clearly with the client.
For more info visit HTTP Status Codes Docs
Security Best Practices
Securing a REST API is critical for production environments. Below are practical measures you can implement in Node.js.
1. Use HTTPS
Always serve your API over HTTPS. SSL/TLS encrypts data in transit, preventing man-in-the-middle attacks.
2. Set Security Headers
Use Helmet to set various HTTP headers that help secure your app.
3. Rate Limiting
Prevent brute-force attacks and abuse by limiting the number of requests from the same IP.
4. Configure CORS
Cross-Origin Resource Sharing (CORS) controls which domains can access your API.
5. Input Validation
Never trust client input. Validate request bodies, params, and query strings. Zod is a great library for this.
6. Authentication & Authorization
Ensure only authenticated users can access protected resources.
7. Secure Error Handling
Do not leak stack traces or sensitive database errors to the client in production.