Built with Express.js, MongoDB, and TypeScript, featuring user authentication, task CRUD operations, and automated task status updates. Includes JWT-based security, Swagger documentation, and Jest unit tests for robust backend functionality.
- Features
- Tech Stack
- Project Structure
- Setup
- Running the Application
- API Endpoints
- Testing
- Scheduled Job
- Contributing
- License
- User registration and login with JWT-based authentication
- Task CRUD operations (create, read, update, delete)
- Edge case handling:
- Prevents duplicate task titles for the same user
- Validates user existence before task creation
- Stores
completedAt
timestamp for completed tasks
- Automated task status updates using
node-cron
(marksin-progress
tasks asdone
after 2 hours) - Swagger/OpenAPI documentation for API exploration
- Unit tests with Jest for the
POST /tasks
endpoint - TypeScript for type safety and maintainable code
- Backend: Express.js, Node.js
- Database: MongoDB with Mongoose
- Language: TypeScript
- Authentication: JWT (jsonwebtoken), bcrypt for password hashing
- Scheduled Tasks: node-cron
- API Documentation: Swagger UI
- Testing: Jest, Supertest
- Environment: dotenv for configuration
task-manager/
├── src/
│ ├── controllers/ # API logic (user, task)
│ ├── models/ # Mongoose schemas (User, Task)
│ ├── routes/ # Express routes
│ ├── middleware/ # JWT authentication middleware
│ ├── jobs/ # node-cron job for auto-closing tasks
│ ├── tests/ # Jest unit tests
│ ├── app.ts # Express app setup
│ ├── server.ts # Server entry point
│ └── swagger.json # Swagger API documentation
├── .env # Environment variables (not tracked)
├── .gitignore # Git ignore file
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── README.md # Project documentation
-
Clone the Repository:
git clone <repository-url> cd task-manager
-
Install Dependencies:
npm install
-
Set Up MongoDB:
- Use a local MongoDB instance or MongoDB Atlas.
- For local MongoDB, ensure the server is running:
mongod
-
Configure Environment Variables:
- Create a
.env
file in the root directory:PORT=3000 MONGO_URI=mongodb://localhost:27017/task-manager JWT_SECRET=your-secret-key
- Replace
MONGO_URI
with your MongoDB connection string (local or Atlas). - Set a secure
JWT_SECRET
for JWT authentication.
- Create a
-
Compile TypeScript:
npx tsc
-
Development Mode (with
ts-node
):npm start
- Requires
ts-node
:npm install -D ts-node
- Requires
-
Production Mode:
npx tsc node dist/server.js
-
Verify:
- Server runs at
http://localhost:3000
(or thePORT
in.env
). - Check console logs:
Connected to MongoDB Server running on port 3000
- Access Swagger UI at
http://localhost:3000/api-docs
.
- Server runs at
The API is documented via Swagger at /api-docs
. Key endpoints:
Method | Endpoint | Description | Authentication |
---|---|---|---|
POST | /users |
Register a new user | None |
POST | /users/login |
Login and obtain JWT token | None |
POST | /tasks |
Create a task | JWT (Bearer Token) |
GET | /tasks?userId=<userId> |
Get tasks for a user | JWT (Bearer Token) |
PATCH | /tasks/:id/status |
Update task status | JWT (Bearer Token) |
DELETE | /tasks/:id |
Delete a task | JWT (Bearer Token) |
-
Register User:
POST /users { "name": "Test User", "email": "[email protected]", "password": "password123" }
-
Login:
POST /users/login { "email": "[email protected]", "password": "password123" }
-
Create Task:
POST /tasks Authorization: Bearer <token> { "title": "My Task", "description": "Task description", "userId": "<userId>" }
- Swagger UI:
- Access
http://localhost:3000/api-docs
to test endpoints interactively.
- Access
- Postman:
- Create a collection for the API.
- Set up an environment with
baseUrl=http://localhost:3000
andtoken
. - Test endpoints in order:
- Register user → Save
userId
. - Login → Save
token
. - Create/Get/Update/Delete tasks using
Bearer {{token}}
.
- Register user → Save
- Test edge cases:
- Duplicate task titles (should return 400).
- Invalid user ID (should return 404).
- Missing/invalid token (should return 401).
- Run Jest tests:
npm test
- Tests are in
src/tests/task.test.ts
for thePOST /tasks
endpoint.
- Use MongoDB Compass or the MongoDB shell to inspect data:
mongo mongodb://localhost:27017/task-manager db.users.find().pretty() db.tasks.find().pretty()
- A
node-cron
job insrc/jobs/autoCloseTasks.ts
runs every 10 minutes. - Marks
in-progress
tasks older than 2 hours asdone
with acompletedAt
timestamp. - To test:
- Modify
autoCloseTasks.ts
to run every minute and check tasks older than 1 minute:cron.schedule('* * * * *', async () => { const oneMinuteAgo = new Date(Date.now() - 60 * 1000); // ... });
- Create an
in-progress
task and verify status change after 1–2 minutes. - Revert changes after testing.
- Modify
- Fork the repository.
- Create a feature branch (
git checkout -b feature/your-feature
). - Commit changes (
git commit -m "Add your feature"
). - Push to the branch (
git push origin feature/your-feature
). - Open a pull request.
This project is licensed under the MIT License.