Back to Tutorials
Node.js
40 min read
Sahasransu Satpathy
3/30/2026
CRUD App Using Node.js & MongoDB
Build a complete CRUD application using Node.js, Express, and MongoDB with step-by-step instructions
Introduction
In this tutorial, you'll build a full CRUD (Create, Read, Update, Delete) application using Node.js, Express, and MongoDB. This app demonstrates practical database integration and REST API development.
Step 1: Project Setup
Initialize your project and install dependencies:
mkdir node-mongo-crud
cd node-mongo-crud
npm init -y
npm install express mongoose nodemon
Add a start script in package.json:
"scripts": {
"start": "nodemon index.js"
}
Step 2: Connect to MongoDB
Create index.js and connect to MongoDB:
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const PORT = 3000;
app.use(express.json());
mongoose.connect('mongodb://localhost:27017/crudApp', {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => console.log('MongoDB connected'))
.catch(err => console.log(err));
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));
Step 3: Create a Mongoose Schema
Create a simple Book model:
const { Schema, model } = mongoose;
const bookSchema = new Schema({
title: { type: String, required: true },
author: { type: String, required: true },
publishedYear: Number
});
const Book = model('Book', bookSchema);
Step 4: REST API Routes
Create a Book
app.post('/books', async (req, res) => {
const book = new Book(req.body);
try {
const savedBook = await book.save();
res.status(201).json(savedBook);
} catch(err) {
res.status(400).json({ message: err.message });
}
});
Read All Books
app.get('/books', async (req, res) => {
try {
const books = await Book.find();
res.json(books);
} catch(err) {
res.status(500).json({ message: err.message });
}
});
Read Book by ID
app.get('/books/:id', async (req, res) => {
try {
const book = await Book.findById(req.params.id);
if(!book) return res.status(404).json({ message: 'Book not found' });
res.json(book);
} catch(err) {
res.status(500).json({ message: err.message });
}
});
Update a Book
app.put('/books/:id', async (req, res) => {
try {
const updatedBook = await Book.findByIdAndUpdate(req.params.id, req.body, { new: true });
res.json(updatedBook);
} catch(err) {
res.status(400).json({ message: err.message });
}
});
Delete a Book
app.delete('/books/:id', async (req, res) => {
try {
await Book.findByIdAndDelete(req.params.id);
res.status(204).send();
} catch(err) {
res.status(500).json({ message: err.message });
}
});
Step 5: Testing
- Use Postman or Insomnia to test all CRUD endpoints.
- Verify creating, reading, updating, and deleting books works.
Step 6: Next Steps
- Add frontend integration with React or Vue.
- Implement user authentication.
- Deploy the app using Heroku or Render.
Conclusion
You now have a fully functional CRUD application using Node.js, Express, and MongoDB. This is a solid foundation for building real-world web applications.
SEO Suggestions:
- Main keywords: Node.js MongoDB tutorial, CRUD app Node.js, Express REST API MongoDB
- Meta description: Learn to build a complete CRUD app using Node.js, Express, and MongoDB. Step-by-step tutorial for beginners with practical examples.
- Catchy title suggestions: "CRUD App Using Node.js & MongoDB – Complete Guide 2026", "Build Full CRUD Applications with Node.js and MongoDB"
Previous Tutorial
Browse All TutorialsNext Tutorial
Browse All Tutorials