A minimal REST API for managing tasks, built with Node.js, Express, and MongoDB (Mongoose).
- Create, read, update, and delete tasks (CRUD)
- JSON request/response
- CORS enabled
- Environment-based MongoDB connection via
MONGO_URI
- Node.js + Express (ES Modules)
- MongoDB + Mongoose
- dotenv, cors, nodemon (dev)
task-manager/
├─ server.js # App entrypoint, Express + routes + DB
├─ package.json # Scripts and deps
├─ models/
│ └─ Task.js # Mongoose Task model
├─ routes/
│ └─ taskRoutes.js # CRUD routes for /api/tasks
└─ README.md
- Node.js 18+ (recommended)
- MongoDB instance (local or hosted)
- Install dependencies
npm install- Configure environment
Create a .env file in the project root with your Mongo connection string:
MONGO_URI=mongodb://localhost:27017/task_manager
- Run the server
- Development (auto-reload):
npm run dev- Production:
npm startThe API listens on http://localhost:5000
Base URL: http://localhost:5000/api/tasks
Task
title(string, required)description(string, optional)status(string, default:"pending")
Example document:
{
"_id": "6738a5f0f1c2a6f5a3d1e999",
"title": "Write docs",
"description": "Create README with API examples",
"status": "pending"
}POST /api/tasks
Request
curl -X POST http://localhost:5000/api/tasks \
-H 'Content-Type: application/json' \
-d '{
"title": "Write docs",
"description": "Create README with API examples"
}'Response 201
{
"_id": "...",
"title": "Write docs",
"description": "Create README with API examples",
"status": "pending"
}GET /api/tasks
curl http://localhost:5000/api/tasksResponse 200
[
{
"_id": "...",
"title": "Write docs",
"description": "Create README with API examples",
"status": "pending"
}
]PUT /api/tasks/:id
curl -X PUT http://localhost:5000/api/tasks/<TASK_ID> \
-H 'Content-Type: application/json' \
-d '{
"status": "done"
}'Response 200
{
"_id": "...",
"title": "Write docs",
"description": "Create README with API examples",
"status": "done"
}DELETE /api/tasks/:id
curl -X DELETE http://localhost:5000/api/tasks/<TASK_ID>Response 200
{ "message": "Task deleted" }On validation or other errors, the API returns:
{ "error": "<message>" }npm run dev— start with nodemon (reload on changes)npm start— start server
- Ensure
MONGO_URIis reachable from your machine/container. - The server currently binds to port
5000. Adjust inserver.jsif needed.
- Input validation (e.g., zod or express-validator)
- Pagination and filtering for GET
/api/tasks - Timestamps on tasks (
createdAt,updatedAt) - Dockerfile and Compose for local Mongo + API
- Tests (Jest + Supertest) and CI workflow
- Authentication (JWT) and per-user tasks
ISC — see package.json.