Skip to main content
Node.js APIs Tutorial
CHAPTER 02 Beginner

Understanding Backend APIs and REST Architecture

Updated: May 14, 2026
15 min read

# CHAPTER 2

Understanding Backend APIs and REST Architecture

1. Introduction

If every developer built APIs their own way, the internet would be a chaotic mess. To ensure that an iPhone, an Android, and a React website can all talk to your Node.js backend seamlessly, developers follow a strict architectural standard called REST (Representational State Transfer). In this chapter, we will learn the rules of REST, the four core HTTP methods, and the standard JSON response format.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define what a REST API is.
  • Understand the Request-Response cycle.
  • Differentiate between the 4 primary HTTP methods (GET, POST, PUT, DELETE).
  • Recognize standard HTTP status codes (200, 201, 404, 500).

3. Beginner-Friendly Explanation

Imagine a library. To manage the library, you have four basic actions:
  1. 1. Read a book.
  1. 2. Add a new book to the shelf.
  1. 3. Update a book's cover.
  1. 4. Throw away a ruined book.
In API terminology, these four actions are called CRUD (Create, Read, Update, Delete). REST is simply a set of rules that dictates exactly which "verb" (HTTP Method) you must use when talking to the librarian (the API) to execute these CRUD actions.

4. The Request-Response Cycle

Every interaction on the internet follows this cycle:
  1. 1. Request: The Client (mobile app) sends an HTTP Request to the Server (Node.js). The request contains a Method (GET/POST), a URL (/users), and sometimes data (a JSON payload).
  1. 2. Response: The Server processes the request and sends back an HTTP Response. This contains a Status Code (e.g., 200 OK) and the requested data (in JSON format).

5. The Four Core HTTP Methods

A REST API uses different HTTP methods to tell the server what to do.
  • GET (Read): Used to retrieve data.
  • *Example:* GET /api/users (Gets all users).
  • POST (Create): Used to send new data to the server to be saved.
  • *Example:* POST /api/users (Creates a new user account).
  • PUT / PATCH (Update): Used to modify existing data.
  • *Example:* PUT /api/users/5 (Updates the profile of user ID 5).
  • DELETE (Delete): Used to remove data.
  • *Example:* DELETE /api/users/5 (Deletes user ID 5).

6. Standardizing the URL Structure

In a REST API, URLs must represent "Resources" (Nouns), not Actions (Verbs).

BAD URL Architecture (Not RESTful):

  • /get-all-users
  • /create-new-user
  • /delete-user-by-id

GOOD URL Architecture (RESTful): Notice how the URL stays exactly the same; the *HTTP Method* changes the action!

  • GET /users (Fetches users)
  • POST /users (Creates a user)
  • DELETE /users/5 (Deletes user 5)

7. HTTP Status Codes

When the server responds, it attaches a 3-digit number. This tells the client exactly what happened without them having to read the text.
  • 200 OK: The request was successful (Used for GET).
  • 201 Created: Data was successfully saved to the database (Used for POST).
  • 400 Bad Request: The user sent invalid data (e.g., missing an email).
  • 401 Unauthorized: The user is not logged in.
  • 404 Not Found: The URL or data does not exist.
  • 500 Internal Server Error: Your Node.js code crashed.

8. The JSON Response Format

APIs communicate primarily using JSON (JavaScript Object Notation). A professional REST API wraps its responses in a standardized format so the frontend knows exactly what to expect.

Example of a standardized JSON Response:

json
12345678
{
  "status": "success",
  "data": {
    "id": 5,
    "name": "Alice",
    "email": "alice@example.com"
  }
}

Example of an Error Response:

json
1234
{
  "status": "error",
  "message": "User not found"
}

9. Best Practices

  • Keep it Stateless: A core rule of REST is that APIs are "stateless." The server should not remember the client between requests (no sessions). Every single request from the client must contain all the information necessary to authenticate it (usually via a JWT token).

10. Common Mistakes

  • Using GET to delete data: Beginners often create routes like GET /api/delete-user/5. This is extremely dangerous. Web crawlers (like Google) click every GET link they find to index the web. If Google crawls your API, it will delete your entire database! Always use the DELETE HTTP method for destructive actions.

11. Exercises

  1. 1. Match the CRUD operation (Create, Read, Update, Delete) to its correct REST HTTP method (GET, POST, PUT, DELETE).

12. Coding Challenges

  • Challenge: Based on REST architectural rules, write out the exact HTTP Method and URL path you would use to fetch a specific blog post with the ID of 42.

13. MCQs with Answers

Question 1

In REST API architecture, which HTTP method should be used when you want to insert a brand new record into the database?

Question 2

What does an HTTP Status Code of 404 signify?

14. Interview Questions

  • Q: Explain the concept of a REST API. What makes an API "RESTful" regarding its URL structure and use of HTTP verbs?
  • Q: Why must REST APIs be "stateless"? How does this differ from traditional web applications that use sessions?

15. FAQs

Q: Are there alternatives to REST? A: Yes. The main modern alternative to REST is GraphQL (created by Facebook), which allows the client to request exactly the specific data fields it wants in a single request. However, REST remains the global industry standard.

16. Summary

In Chapter 2, we learned the grammar of the internet. By strictly adhering to REST architecture, we ensure our Node.js APIs are predictable and professional. By mapping CRUD actions to HTTP methods (POST, GET, PUT, DELETE) and returning standardized HTTP Status codes and JSON payloads, we guarantee that any frontend developer can seamlessly integrate with our backend.

17. Next Chapter Recommendation

Now that we know the rules, we need the tools to build it. Proceed to Chapter 3: Setting Up Node.js Development Environment.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·