Building a To-Do App Using MERN Stack
The MERN stack — consisting of MongoDB, Express.js, React, and Node.js — is a popular combination of technologies for building full-stack web applications using JavaScript. One of the best ways to learn the MERN stack is by building a simple To-Do application. This blog provides a step-by-step overview of how to build a basic To-Do app using the MERN stack.
Project Setup
Start by setting up the project structure with separate directories for the client (React) and server (Node/Express).
npx create-react-app client
mkdir server
cd server
npm init -y
npm install express mongoose cors
Also, install nodemon for automatic server reload during development.
Backend: Node.js + Express + MongoDB
Create a MongoDB database using MongoDB Atlas or locally.
Define a Mongoose schema for your To-Do items:
const mongoose = require('mongoose');
const TodoSchema = new mongoose.Schema({
text: String,
completed: Boolean
});
module.exports = mongoose.model('Todo', TodoSchema);
Set up REST API routes using Express:
app.get('/todos', getTodos);
app.post('/todos', addTodo);
app.put('/todos/:id', updateTodo);
app.delete('/todos/:id', deleteTodo);
These routes handle CRUD operations and communicate with the MongoDB database.
Frontend: React.js
In the client directory, use axios to make HTTP requests to the backend:
npm install axios
Create components like:
- TodoList – displays all to-do items
- TodoForm – handles new task input
- TodoItem – shows individual to-dos with edit/delete functionality
Use React Hooks (useState, useEffect) to manage state and API calls.
Connecting Frontend and Backend
Use CORS in the backend to allow the frontend to make requests:
const cors = require('cors');
app.use(cors());
Set up proxy in client/package.json to point to the backend server for seamless API communication.
Final Touches
Run both servers simultaneously using tools like concurrently for development:
npm install concurrently --save-dev
Now you have a functional full-stack To-Do app where users can:
- Add new tasks
- View all tasks
- Mark tasks as completed
- Delete tasks
Conclusion
Building a To-Do app using the MERN stack gives you hands-on experience with each layer of a full-stack application. It’s a great project for mastering CRUD operations, REST APIs, React components, and MongoDB integration. Once completed, you can further enhance the app with user authentication, due dates, or even deploy it using platforms like Heroku or Vercel.
Learn MERN Stack Training Course
Read More:
Exploring the Basics of Express.js for Beginners
How to Create Your First MERN Stack App
Managing State in React with Context API
Deploying Your MERN App on Vercel
CRUD Operations Using MERN Stack
Visit Quality Thought Training Institute
Comments
Post a Comment