A simple RESTful API for managing tasks, built with Node.js, Express, and MongoDB (using Mongoose). This project is designed for learning purposes and demonstrates how to create, read, update, and delete (CRUD) tasks in a MongoDB database.
- Create Tasks: Add new tasks with a title and description.
- Read Tasks: Retrieve all tasks or a specific task by its ID.
- Update Tasks: Modify existing tasks by ID.
- Delete Tasks: Remove tasks from the database by ID.
- Data Validation: Ensures required fields are present and properly formatted.
- Error Handling: Graceful error responses for invalid requests or server issues.
-
Clone the repository:
git clone https://github.com/your-username/task-manager.git cd task-manager -
Install dependencies:
npm install
-
Start MongoDB:
Make sure your MongoDB server is running. By default, the app connects tomongodb://localhost:27017/taskdb. -
Run the server:
node server.js
The server will start on port 3000.
- POST
/tasks - Body Example:
{ "title": "Learn Node.js", "description": "Study Express and Mongoose basics" } - Response: Returns the created task object.
- GET
/tasks - Response: Returns an array of all tasks.
- GET
/tasks/:id - Response: Returns the task object with the specified ID.
- PUT
/tasks/:id - Body Example:
{ "completed": true } - Response: Returns the updated task object.
- DELETE
/tasks/:id - Response: Returns a success message if the task is deleted.
task-manager/
├── server.js # Entry point, sets up Express and MongoDB connection
├── task.js # Mongoose schema/model for tasks
├── routes.js # Express routes for task CRUD operations
├── package.json # Project metadata and dependencies
└── README.md # Project documentation
- How to structure a Node.js REST API project
- Using Mongoose for schema definition and data validation
- Implementing CRUD operations with Express routes
- Handling errors and sending appropriate HTTP responses
This project is for educational purposes only.
Happy coding! 🚀