The Task Manager API is a simple RESTful API built with Node.js and Express.js that allows users to create, retrieve, update, and delete tasks. It integrates with MongoDB for data storage and follows best practices for modular code organization.
task-manager-api
┣ 📂 node_modules # Installed dependencies
┣ 📂 src
┃ ┣ 📂 routes # All route handlers
┃ ┣ 📂 controllers # Business logic (separate from routes)
┃ ┣ 📂 middleware # Custom middleware (e.g., logging)
┃ ┣ 📂 config # Configurations (DB, environment variables)
┃ ┣ 📂 models # Data models (if using a database)
┃ ┣ 📜 app.js # Express setup & middleware
┃ ┗ 📜 server.js # Server startup file
┣ 📜 .env # Environment variables (not committed)
┣ 📜 .gitignore # Ignore unnecessary files
┣ 📜 package-lock.json # Project dependencies
┣ 📜 package.json # Project metadata & scripts
┣ 📜 README.md # Project documentation
git clone https://github.com/your-username/task-manager-api.git
cd task-manager-api
npm install
Create a .env
file in the project root and add the following:
MONGO_URI=mongodb+srv://<username>:<password>@cluster0.mongodb.net/taskmanager?retryWrites=true&w=majority
PORT=3000
Replace <username>
and <password>
with your actual MongoDB credentials.
npm run dev
This will start the server at
http://localhost:3000
Method | Endpoint | Description |
---|---|---|
GET | /api/tasks |
Retrieve all tasks |
GET | /api/tasks/:id |
Get a specific task by ID |
POST | /api/tasks |
Create a new task |
PUT | /api/tasks/:id |
Update a task |
DELETE | /api/tasks/:id |
Delete a task |
import mongoose from 'mongoose';
const taskSchema = new mongoose.Schema({
title: { type: String, required: true },
description: { type: String },
completed: { type: Boolean, default: false }
}, { timestamps: true });
const Task = mongoose.model('Task', taskSchema);
export default Task;
Request:
POST /api/tasks
Content-Type: application/json
{
"title": "Learn MongoDB",
"description": "Understand database CRUD operations"
}
Response:
{
"_id": "65d8e0b6c8f1a9d12a7b33b9",
"title": "Learn MongoDB",
"description": "Understand database CRUD operations",
"completed": false,
"createdAt": "2024-02-28T12:00:00Z",
"updatedAt": "2024-02-28T12:00:00Z"
}
GET /api/tasks
PUT /api/tasks/65d8e0b6c8f1a9d12a7b33b9
Content-Type: application/json
{
"title": "Learn Mongoose",
"completed": true
}
DELETE /api/tasks/65d8e0b6c8f1a9d12a7b33b9
✅ Node.js – Backend runtime
✅ Express.js – API framework
✅ MongoDB – Database
✅ Mongoose – ODM (Object Data Modeling)
✅ Dotenv – Manage environment variables
✅ Cors & Morgan – Middleware for API security & logging
- Fork the Repository
- Create a New Branch
- Make Changes & Commit
- Push to GitHub
- Open a Pull Request
Use Postman or cURL to send requests to API endpoints.
Ensure your MONGO_URI in .env
is correct and the database is accessible.
Press CTRL + C
in the terminal.
🚀 Happy Coding! Let me know if you need any modifications! 😊
---