Introduction 

Building a robust backend API is a fundamental skill for modern web developers. In this tutorial, you’ll learn how to build a complete CRUD (Create, Read, Update, Delete) API using Node.js, Express, and MongoDB Atlas. By the end of this guide, you’ll have a fully functional RESTful API connected to a cloud database, ready for production use.

Why Use Node.js, Express, and MongoDB?

Before we dive in, let’s briefly understand why this stack is so popular:

  • Node.js: Enables fast, scalable server-side applications using JavaScript.
  • Express.js: A lightweight framework that simplifies API development.
  • MongoDB Atlas: A fully managed cloud database that is easy to scale and maintain.

Now that you understand the benefits, let’s move step-by-step toward building your API.

Prerequisites

Make sure you have the following installed:

  • Node.js 
  • npm or yarn
  • MongoDB Atlas account
  • Postman (for API testing)

Before jumping into code, it’s important to visualize how everything connects.

With everything set up, let’s start by creating the project.

Step 1: Initialize the Node.js Project

Start by setting up a Node.js project and installing the core dependencies required to build your API using the code below.

Next, install the following required dependencies

At this stage, your project is ready to be configured.

Step 2: Project Structure

Organize your folders using a scalable architecture (MVC-style) to keep your code maintainable. Here’s a recommended structure:

Now that the structure is clear, let’s connect to MongoDB Atlas.

Step 3: Connect to MongoDB Atlas

Establish a secure connection to your cloud database using Mongoose and environment variables. Create a .env file and add your connection string:

Then create a database connection file:

config/db.js

With the database connection ready, let’s move to creating the model.

Step 4: Create a Mongoose Model

Define your schema using Mongoose to structure and validate the data stored in MongoDB. We’ll create a simple “User” model.

models/User.js

Now that the model is defined, let’s build the controller logic.

Step 5: Create CRUD Controllers

Implement the business logic for CRUD operations and database interaction. controllers/userController.js

With the controllers in place, the next step is to define the routes.

Step 6: Create Routes

Map HTTP endpoints to controller functions to expose your API functionality.

routes/userRoutes.js

Now that routing is configured, let’s wire everything together in the server.

Step 7: Setup Express Server

Configure middleware, connect the database, and start your server to handle incoming requests.

server.js

At this point, your API is fully set up. With the API running, let’s visualize real-world interactions.

Step 8: Test the API

Use Postman or any API client:

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

If everything is working as expected, congratulations, you’ve built a complete CRUD API!

Testing the API Using Console (Without Postman)

If you don’t have Postman installed, you can still test your API directly from the terminal using curl commands.

Make sure your server is running before executing these commands.

1. Create a User (POST)

2. Get All Users (GET)

3. Get a Single User (GET)

Replace <USER_ID> with an actual ID from your database.

4. Update a User (PUT)

5. Delete a User (DELETE)

Best Practices and Optimization Tips

To take your API to the next level, consider the following:

  • Add validation using libraries like Joi or express-validator
  • Implement proper error-handling middleware
  • Use environment variables for configuration
  • Add authentication (JWT)
  • Structure your project using MVC architecture

Conclusion

In this tutorial, you learned how to build a scalable CRUD API using Node.js, Express, and MongoDB Atlas. You set up a project, connected to a cloud database, created models, implemented controllers, and exposed RESTful endpoints.

As you continue building APIs, you can extend this project by adding authentication, pagination, and advanced query filtering.