Skip to main content

Command Palette

Search for a command to run...

REST in Node.js — A Friendly Guide to Methods, Status Codes & Routes

Updated
•5 min read•View as Markdown

Modern web applications constantly exchange data between clients and servers. Whether you're logging into an app, fetching products from an e-commerce site, or posting a tweet, APIs make that communication possible.

One of the most common styles used for building APIs is REST.

In this article, you'll learn:

  • What REST API means

  • What resources are in REST architecture

  • HTTP methods (GET, POST, PUT, DELETE)

  • Basic HTTP status codes

  • How to design clean REST routes

  • A complete users resource example


What is a REST API?

A REST API (Representational State Transfer API) is a way for applications to communicate over HTTP using standard rules.

In simple words:

  • The client sends a request

  • The server processes it

  • The server sends back a response

Example:

GET /users/1

The client asks:

“Give me the user whose ID is 1.”

The server responds with data:

{
  "id": 1,
  "name": "Vaibhav"
}

API as Communication Between Client and Server

Think of an API like a waiter in a restaurant.

  • The client = customer

  • The server = kitchen

  • The API = waiter carrying requests and responses

Flow:

  1. Customer places order

  2. Waiter takes order to kitchen

  3. Kitchen prepares food

  4. Waiter returns response

Similarly:

  1. Client sends HTTP request

  2. API receives request

  3. Server processes data

  4. API sends HTTP response


What are Resources in REST?

In REST architecture, everything is treated as a resource.

Examples:

  • users

  • products

  • orders

  • posts

  • comments

Each resource has its own URL.

Example:

/users
/products
/orders

A single resource item is usually identified using an ID:

/users/1
/products/25

HTTP Methods in REST APIs

REST APIs use HTTP methods to define actions.


1. GET → Read Data

Used to fetch data from the server.

Example:

GET /users

Fetch all users.

Example:

GET /users/1

Fetch one user.

Typical Response

{
  "id": 1,
  "name": "Vaibhav"
}

2. POST → Create Data

Used to create a new resource.

Example:

POST /users

Request body:

{
  "name": "Rahul",
  "email": "rahul@example.com"
}

Server Response

{
  "message": "User created"
}

3. PUT → Update Data

Used to update an existing resource.

Example:

PUT /users/1

Request body:

{
  "name": "Updated Name"
}

This updates user with ID 1.


4. DELETE → Remove Data

Used to delete a resource.

Example:

DELETE /users/1

Deletes the user whose ID is 1.


CRUD Operations vs HTTP Methods

CRUD Operation HTTP Method Example Route
Create POST /users
Read GET /users
Update PUT /users/1
Delete DELETE /users/1

REST Request–Response Lifecycle

Client
   |
   | HTTP Request
   v
REST API Server
   |
   | Process Logic / Database
   v
HTTP Response
   |
   v
Client

Designing RESTful Routes

Good REST APIs use clean and predictable route naming.

Good Route Design

GET /users
GET /users/1
POST /users
PUT /users/1
DELETE /users/1

Notice:

  • Resource names are plural

  • URLs represent resources

  • HTTP methods represent actions


Bad Route Design

/getUsers
/createUser
/deleteUserById

Why bad?

Because actions are already represented by HTTP methods.

REST focuses on:

  • Clean URLs

  • Consistent structure

  • Predictable behavior


Understanding Status Codes

HTTP status codes tell the client what happened.


Common Status Codes

200 OK

Request succeeded.

Example:

GET /users

201 Created

Resource successfully created.

Example:

POST /users

404 Not Found

Requested resource does not exist.

Example:

GET /users/999

500 Internal Server Error

Something failed on the server.

Example:

  • Database crash

  • Unexpected server error


Example Users REST API

Here’s how a simple users API may look:


Get All Users

GET /users

Response:

[
  {
    "id": 1,
    "name": "Vaibhav"
  },
  {
    "id": 2,
    "name": "Rahul"
  }
]

Get Single User

GET /users/1

Create User

POST /users

Body:

{
  "name": "Ankit"
}

Update User

PUT /users/1

Body:

{
  "name": "Updated User"
}

Delete User

DELETE /users/1

Why REST APIs Became Popular

REST APIs are widely used because they are:

  • Simple

  • Lightweight

  • Easy to understand

  • Language independent

  • Scalable

  • Compatible with web standards

Almost every modern frontend communicates with backend servers using REST APIs.


Real-World Examples

Many applications use REST APIs internally:

  • Social media apps

  • E-commerce websites

  • Banking dashboards

  • Mobile apps

  • Admin panels

Example flow:

Frontend → REST API → Database

Final Thoughts

REST APIs are the foundation of modern web communication.

Key takeaways:

  • REST treats data as resources

  • URLs identify resources

  • HTTP methods define actions

  • Status codes explain results

  • Clean route naming improves API design

Once you understand REST principles, learning frameworks like Express.js becomes much easier.

4 views