A RESTful API for a Todo List application built with Express.js, TypeScript, and MongoDB.
- Create new tasks
- List all tasks (with optional filtering by status)
- Update existing tasks
- Delete tasks
- Node.js
- Express.js
- TypeScript
- MongoDB with Mongoose
- Node.js (v14 or higher)
- MongoDB Atlas account or local MongoDB instance
- Clone the repository:
git clone https://github.com/singharyan006/todo-backend.git
cd todo-backend- Install dependencies:
npm install- Set up environment variables:
Create a
.envfile in the root directory with the following variables:
PORT=3000
MONGODB_URI=your_mongodb_connection_string
- Create a free account at MongoDB Atlas
- Create a new cluster
- In "Database Access," create a new user with read/write privileges
- In "Network Access," allow access from your IP or from anywhere (0.0.0.0/0)
- Click "Connect" and choose "Connect your application"
- Copy the connection string and replace
<username>and<password>with your credentials - Add this connection string to your
.envfile
- Install and start MongoDB locally
- Set
MONGODB_URI=mongodb://127.0.0.1:27017/todoappin your.envfile
npm run devnpm run buildnpm startThe codebase follows a structured approach:
src/server.ts- Main entry point that connects to MongoDB and starts the serversrc/index.ts- Sets up the Express application with middleware and routessrc/models/Task.ts- Task model definition with schemasrc/controllers/taskController.ts- Request handlers for CRUD operationssrc/routes/taskRoutes.ts- API route definitions
The code includes detailed comments to help beginners understand how the application works.
- Method: POST
- Endpoint:
/tasks - Body:
{ "title": "Buy groceries", "description": "Milk, Eggs, Bread", "status": "pending" }
- Method: GET
- Endpoint:
/tasks - Query Parameters:
status: (optional) - Filter by "pending" or "completed"
- Method: PUT
- Endpoint:
/tasks/:id - Body (all fields optional):
{ "title": "Updated title", "description": "Updated description", "status": "completed" }
- Method: DELETE
- Endpoint:
/tasks/:id
PORT: The port the server will run on (default: 3000)MONGODB_URI: MongoDB connection string