Building API with MongoDB, Express.js, and Node.js
Building REST API with MongoDB, Express.js, and Node.js
Building a REST API with MongoDB, Express.js, and Node.js is a complete backend development process. This combination is commonly known as the MERN backend stack. In this lesson, you will learn how to connect MongoDB with Express.js and Node.js to create a fully functional API.
Setup Project for API Development
Install Required Packages
npm init -y
npm install express mongoose
Basic Server Setup
const express = require(‘express’);
const mongoose = require(‘mongoose’);
const app = express();
app.use(express.json());
app.listen(3000, () => {
console.log(‘Server running on port 3000’);
});
Connect MongoDB with Mongoose
Database Connection
mongoose.connect(‘mongodb://localhost:27017/mydb’)
.then(() => console.log(‘MongoDB connected’))
.catch(err => console.log(err));
Create Schema and Model
User Schema
const userSchema = new mongoose.Schema({
name: String,
age: Number
});
User Model
const User = mongoose.model(‘User’, userSchema);
Create REST API Routes
Create User API (POST)
app.post(‘/users’, async (req, res) => {
const user = new User(req.body);
await user.save();
res.json(user);
});
Get All Users API (GET)
app.get(‘/users’, async (req, res) => {
const users = await User.find();
res.json(users);
});
Get Single User API (GET by ID)
app.get(‘/users/:id’, async (req, res) => {
const user = await User.findById(req.params.id);
res.json(user);
});
Update User API (PUT)
app.put(‘/users/:id’, async (req, res) => {
const user = await User.findByIdAndUpdate(req.params.id, req.body, { new: true });
res.json(user);
});
Delete User API (DELETE)
app.delete(‘/users/:id’, async (req, res) => {
await User.findByIdAndDelete(req.params.id);
res.send(‘User deleted’);
});
Complete API Flow
This API allows you to create, read, update, and delete users using MongoDB, Express.js, and Node.js. It is a real-world backend structure used in many applications.
Best Practices for Building API
Use Async Await
Handle database operations properly
Validate Data
Always validate user input
Use Proper Status Codes
Send correct HTTP responses
Organize Code
Separate routes, models, and controllers
Real-World Use of MERN Backend
This structure is used in applications like e-commerce platforms, admin dashboards, and mobile apps. It is one of the most in-demand backend skills.
FAQs on Building API with MongoDB and Express.js
How to build API in Node.js
You can build API using Express.js and connect it with MongoDB using Mongoose.
What is MERN stack
MERN stack includes MongoDB, Express.js, React, and Node.js.
Is MongoDB good for API development
Yes, MongoDB is widely used for building scalable APIs.
Continue Learning Backend Development
Now that you understand how to build a REST API with MongoDB, Express.js, and Node.js, you are ready to move to the next lesson where you will learn about authentication and JWT in Node.js. To explore more structured tutorials and courses, click here for more free courses.



