
code . automatically opens VS Code. If it doesn’t, open VS Code manually and navigate to your mern-todo-app folder.mern-todo-app), then run the following command:frontend folder within your mern-todo-app directory.src folder, create an App.tsx file if it doesn’t already exist, and add the below code. It’s a lot, but don’t worry – I’ll break it down bit by bit afterwards:frontend/src/App.tsx:React, { useState, useEffect }: Manages component state and side effects.axios: Handles API requests.TodoList.tsx: A child component to display and manage tasks.App.css: Styles the app._id, title, completed).tasks: Stores the list of tasks.task: Holds input for new tasks.editingTaskId: Tracks the task being edited.editingTitle: Stores the updated title while editing.useEffect)GET /api/tasks) to get tasks and updates tasks.POST request to add a new task.tasks with the new task.DELETE request to remove a task.tasks by filtering out the deleted task.PUT request to update a task’s title.tasks with the new title.startEditing(id): Sets a task into edit mode.handleEditChange(e): Updates the editing input.deleteTask, updateTask, etc.) to TodoList.tsx.export default App;: Makes App usable in other files.src folder, create a new folder named components. Then add a TodoList.tsx file inside it with the below code.src/components/TodoList.tsx:_id, title, and completed properties.TodoList componentTodoList componentTodoList) using TypeScript (React.FC<TodoListProps>).TodoListProps and prepares the component for rendering.tasks to display each task inside a <ul>.completed status using updateTask().updateTask(), then exits edit mode.deleteTask() to remove a task.editingTaskId and editingTitle.TodoList available for use in other components.src folder, create App.css if it doesn’t exist and replace the content with your desired styling. Let’s give the frontend a finishing touch.src/App.css:html, body):flexbox to center the app vertically and horizontally.height: 100vh for full-screen height.overflow-x: hidden..App):width: 90% and max-width: 350px.Arial, sans-serif as the font.h1).span { flex: 1; }.background-color: #0056b3).ul, li, span.completed):li) has a white background, padding, rounded corners, and a shadow.line-through and faded text color.accent-color: #007bff).editing-container with display: flex; for edit mode.@media (max-width: 400px)):.App width and padding for small screens.li) vertically instead of side-by-side.mern-todo-app) and then create a folder called backend. Navigate to the backend folder and initialize Node.js:backend folder, run this command:express is a fast and minimal web framework for Node.js used to create server-side applications and APIs.mongoose is an Object Data Modeling (ODM) library for MongoDB, simplifying database interactions.dotenv loads environment variables from a .env file, keeping sensitive data secure.cors enables Cross-Origin Resource Sharing, allowing frontend applications to communicate with the backend across different domains.backend folder, create a file named server.js and enter the following code:backend/server.js:dotenv.config().express() to create an app instance.connectDB() to connect to MongoDB using MONGO_URI.tasksRoutes from ./routes/tasks./api/tasks as the base route for task operations.PORT from .env or defaults to 5000.backend folder, create a model folder. Inside model, create a file named Task.js and add the following code:backend/model/Task.js:mongoose: Used to define the schema and interact with MongoDB.title: A required string field for the task title.completed: A boolean field indicating task status (default: false)."Task" based on TaskSchema.backend folder, create a routes folder. Inside routes, create a file named tasks.js and add the following code:backend/routes/tasks.js:title from the request body.id from the request params.id from the request params.router for use in other parts of the app.Task model using MongoDB. It defines routes to get all tasks, add a new task, delete a task by ID, and update a task's title by ID. Error handling ensures proper responses for missing data or server issues..env file and add the following:backend/.env:your_mongodb_atlas_uri in .env file with your MongoDB Atlas connection string.npm run dev, you need to install a dependency that will start both the frontend and backend simultaneously. You can do this using concurrently.mern-todo-app), and run:package.json file in your project root directory. If it doesn't exist, create one and add the following code:mern-todo-app/package.json:package.json file configures the application by defining:name, version, private flag).start, client, and dev) to start the backend, run the frontend, and execute both simultaneously.concurrently, which enables running multiple scripts in parallel.Posted Jul 14, 2026
Built a MERN stack To-Do app covering CRUD operations and MongoDB integration.