RESTful API for task management built with Node.js, Express, and MongoDB.
- User authentication with JWT
- User registration and login (username or email)
- Task management (CRUD operations)
- Task filtering by status and priority
- Secure password storage with bcrypt
- Input validation with Joi
- Node.js: JavaScript runtime
- Express: Web framework
- MongoDB: NoSQL database
- Mongoose: MongoDB object modeling
- JWT: Authentication method
- bcrypt: Password hashing
- Joi: Input validation
task-management-api/
│
├── src/
│ ├── config/ # Configuration files
│ ├── controllers/ # Request handlers
│ ├── middleware/ # Custom middleware
│ ├── models/ # Database models
│ ├── routes/ # API routes
│ ├── utils/ # Utility functions
│ └── app.js # Application entry point
│
├── .env # Environment variables
├── LICENSE # The license file
├── package.json # Project dependencies
└── README.md # Project documentation
- Node.js (v14.x or later)
- MongoDB (local instance or MongoDB Atlas)
- Clone the repository:
git clone https://github.com/yourusername/task-management-api.git
cd task-management-api
- Install dependencies:
npm install
- Create a
.env
file in the root directory:
MONGODB_URI=mongodb://localhost:27017/task_management_db
JWT_SECRET=your_very_long_and_complex_secret_key_here
JWT_EXPIRES_IN=1h
PORT=3000
- Start the development server:
npm run dev
Method | Endpoint | Description |
---|---|---|
POST | /api/auth/register |
Register a new user |
POST | /api/auth/login |
Login a user |
Method | Endpoint | Description |
---|---|---|
GET | /api/tasks |
Get all tasks for authenticated user |
POST | /api/tasks |
Create a new task |
PUT | /api/tasks/:id |
Update a task |
DELETE | /api/tasks/:id |
Delete a task |
Request:
POST /api/auth/register
{
"username": "johndoe",
"email": "john@example.com",
"password": "securepassword123"
}
Response:
{
"message": "User registered successfully",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": "60f1a5c5c5b5e91234a56789",
"username": "johndoe",
"email": "john@example.com"
}
}
Request:
POST /api/tasks
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
{
"title": "Complete project documentation",
"description": "Write detailed README for the task management API",
"priority": "high",
"status": "in-progress"
}
Response:
{
"_id": "60f1b6d7e8f9a01234567890",
"title": "Complete project documentation",
"description": "Write detailed README for the task management API",
"priority": "high",
"status": "in-progress",
"user": "60f1a5c5c5b5e91234a56789",
"createdAt": "2023-07-16T12:30:15.123Z",
"updatedAt": "2023-07-16T12:30:15.123Z"
}
- Passwords are hashed using bcrypt
- Authentication is handled via JWT
- Sensitive data is stored in environment variables
- Input validation prevents malicious data
The API returns appropriate HTTP status codes:
200
- Success201
- Resource created400
- Bad request (validation error)401
- Unauthorized (authentication error)404
- Resource not found500
- Server error
MIT
Michal Tarnowski