Building REST APIs

Node js 12 min min read Updated: Mar 30, 2026 Intermediate
Building REST APIs
Intermediate Topic 5 of 10

Building REST APIs in Express.js

REST APIs are one of the most common ways to build backend services in modern web applications. They allow communication between client applications (like frontend apps or mobile apps) and the server using standard HTTP methods.

In Express.js, building REST APIs is simple and efficient. You define routes for different operations such as retrieving data, creating new records, updating existing data, and deleting resources.

Key Concept: REST APIs use HTTP methods like GET, POST, PUT, and DELETE to perform operations on resources.

What is a REST API?

REST stands for Representational State Transfer. It is an architectural style used to design APIs that communicate over HTTP.

In a REST API, everything is treated as a resource, such as users, products, or orders. Each resource is identified by a URL, and operations are performed using HTTP methods.

HTTP Methods Used in REST APIs

  • GET: The GET method is used to request and retrieve data from the server without making any changes to it. It is commonly used to fetch resources such as user lists, product details, or pages. GET requests are read-only and can be cached and bookmarked.
  • POST: The POST method is used to send data to the server in order to create a new resource. It is typically used when submitting forms, registering users, or adding new records to a database. The data is sent in the request body and is not visible in the URL.
  • PUT: The PUT method is used to update an existing resource on the server. It replaces the entire resource with the new data provided in the request. It is commonly used when editing user profiles or updating records in a database.
  • DELETE: The DELETE method is used to remove a resource from the server permanently. It is typically used to delete records such as users, products, or files from a database.

Basic REST API Example

javascript const express = require("express"); const app = express(); app.use(express.json()); // GET all users app.get("/users", (req, res) => { res.json([{ id: 1, name: "Rahul" }]); }); // POST create user app.post("/users", (req, res) => { res.json({ message: "User created" }); }); // PUT update user app.put("/users/:id", (req, res) => { res.json({ message: "User updated" }); }); // DELETE user app.delete("/users/:id", (req, res) => { res.json({ message: "User deleted" }); }); app.listen(3000);

This example demonstrates a basic REST API with CRUD (Create, Read, Update, Delete) operations.

Understanding Each Operation

GET – Fetch Data

The GET method is used to retrieve data from the server. It does not modify any data.

javascript app.get("/users", (req, res) => { res.json(users); });

POST – Create Data

The POST method is used to send data to the server and create a new resource.

javascript app.post("/users", (req, res) => { const newUser = req.body; res.json(newUser); });

PUT – Update Data

The PUT method is used to update an existing resource using its ID.

javascript app.put("/users/:id", (req, res) => { res.json({ message: "User updated" }); });

DELETE – Remove Data

The DELETE method is used to remove a resource from the server.

javascript app.delete("/users/:id", (req, res) => { res.json({ message: "User deleted" }); });

REST API Structure

A well-structured REST API follows consistent naming and organization:

  • GET /users → Get all users
  • GET /users/:id → Get single user
  • POST /users → Create user
  • PUT /users/:id → Update user
  • DELETE /users/:id → Delete user

Status Codes in REST APIs

  • 200: Success
  • 201: Created
  • 400: Bad Request
  • 404: Not Found
  • 500: Server Error
javascript res.status(201).json({ message: "Created" });

Best Practices for REST APIs

  • Use meaningful route names
  • Follow REST conventions consistently
  • Validate input data
  • Use proper status codes
  • Handle errors gracefully

Common Mistakes

  • Not using proper HTTP methods
  • Returning incorrect status codes
  • Mixing business logic with routes
  • Not validating request data

Real-World Use Cases

  • Backend for web applications
  • Mobile app APIs
  • Microservices architecture
  • Third-party integrations

Conclusion

REST APIs are the backbone of modern web applications. With Express.js, you can quickly build scalable and maintainable APIs using simple and readable code.

By mastering REST API concepts, you can build powerful backend systems and connect them with frontend applications seamlessly.

Quick Summary: REST APIs use HTTP methods like GET, POST, PUT, and DELETE to perform operations on resources in a structured way.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators