CHAPTER 10
Beginner
Building CRUD Applications with Express.js
Updated: May 14, 2026
35 min read
# CHAPTER 10
Building CRUD Applications with Express.js
1. Introduction
Up to this point, we have learned the individual puzzle pieces of Express: routing, handling JSON payloads, URL parameters, and middleware. In this chapter, we will synthesize everything. We will build a complete CRUD (Create, Read, Update, Delete) API. To adhere to professional standards, we will move our logic out of the main server file and introduce the MVC (Model-View-Controller) pattern to our Node.js backend.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand how to structure an Express app using Controllers and modular Routers.
- Build the 5 standard REST endpoints for a resource.
- Simulate a database using an in-memory JavaScript array.
- Implement full CRUD operations handling JSON requests and responses.
3. Beginner-Friendly Explanation
If you put all your clothes, kitchen utensils, and power tools in one giant box in your living room, you would never find anything. This is what anindex.js file looks like when you write 50 routes containing 500 lines of logic.
To fix this, we build "Rooms."
-
Routes (
routes/bookRoutes.js): The hallway signs. "Go to the kitchen."
-
Controllers (
controllers/bookController.js): The actual rooms where the work happens.
4. Project Setup & Simulated Database
For this chapter, we will build an API to manage Books. Because we haven't connected a real database yet (that is the next chapter), we will use an array of objects to act as our temporary "database."5. Step 1: The Controller (The Logic)
Create a new folder calledcontrollers and a file named bookController.js.
Here, we write functions for all 5 CRUD actions and export them.
javascript
6. Step 2: The Router (The Hallway Signs)
Create a new folder calledroutes and a file named bookRoutes.js.
Notice how incredibly clean this file is! It just maps a URL to a specific Controller function.
javascript
7. Step 3: Wiring it to index.js
Finally, connect the modular router to your main server file.
javascript
8. Backend Workflow: Reviewing the Flow
Let's trace a complete request:-
1.
Mobile App sends
POST /api/bookswith JSON data.
-
2.
The Request hits
index.js.
-
3.
express.json()middleware parses the data intoreq.body.
-
4.
index.jssees the URL starts with/api/books, so it hands traffic control tobookRoutes.
-
5.
bookRoutessees it is a POST request to/, so it triggersbookController.createBook.
- 6. The Controller function extracts the data, validates it, adds it to the array, and returns a 201 Status Code with the new JSON object.
9. Best Practices
-
Consistent File Structure: Always separate your concerns.
index.jsconfigures the server.routes/defines the URLs.controllers/contains the logic. This architecture is universally recognized by professional Node developers and makes scaling massive applications possible.
10. Common Mistakes
-
parseInt vs String: In the URL
/api/books/1, the1is captured inreq.params.idas a String ("1"). In our array, the ID is an Integer (1). If you try to find a match using strict equality (===) without converting the string to an integer usingparseInt(), JavaScript will fail to find the book.
11. Exercises
- 1. Explain the architectural advantages of moving business logic out of the Router files and into dedicated Controller files.
12. Coding Challenges
-
Challenge: Using Postman, craft the JSON payload required to test the
updateBook(PUT) route. What URL do you use? What body format do you select? What HTTP method do you select?
13. MCQs with Answers
Question 1
When building a RESTful CRUD API, which HTTP Status Code is the industry standard response for a successful DELETE operation where no JSON data needs to be returned?
Question 2
In a well-structured MVC Node.js API, what is the primary responsibility of a Controller file?
14. Interview Questions
- Q: Explain the Separation of Concerns in an Express.js API regarding the Router layer and the Controller layer.
- Q: Walk me through the 5 standard RESTful endpoints for a resource named "articles", including the HTTP verb and the URL path for each.