A simple, secure Todo API with authentication and full CRUD support.
- 🔐 User Authentication – Register and log in with secure JWT-based auth
- 🛡️ Protected API Routes – Access control for authenticated users only
- ✏️ Full Todo CRUD – Create, Read, Update, and Delete todos seamlessly
- 🗃️ MongoDB + Mongoose – Schema-based data modeling
- 🛠️ Robust Validation & Error Handling – Clean error responses and field checks
Backend:
- 🟢 Node.js v18 or higher
- 🍃 MongoDB URI (local or MongoDB Atlas)
- 🔍 API Client like Postman or Thunder Client for testing
Follow these steps to get started with this template:
1. Clone the repository
git clone https://github.com/peyush-nuwal/todo-api.git2. Navigate to the project directory
cd todo-api3. Install dependencies
npm installOr use
yarnorpnpmif you prefer.
4. Create a .env file and add the following:
PORT=5000
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_super_secret_key5. Start the server in development mode
npm run devThe API will be running at:
http://localhost:5000/api
All routes are prefixed with:
http://localhost:5000/api
| Method | Endpoint | Description |
|---|---|---|
| POST | /register |
Register new user |
| POST | /login |
Login and get JWT |
Request Body for /register and /login:
{
"email": "user@example.com",
"password": "yourpassword"
}| Method | Endpoint | Description |
|---|---|---|
| GET | /todos/ |
Get all todos for user |
| POST | /todos/ |
Create a new todo |
| PUT | /todos/:id |
Update a todo by ID |
| DELETE | /todos/:id |
Delete a todo by ID |
Example Todo Payload::
{
"title": "Buy groceries",
"description": "Milk, eggs, bread",
"status": "pending"
}🔒 For protected routes, include the token in the
Authorizationheader as:
Bearer your_jwt_token_here
todo-api/
├── config/ # DB connection config
│ └── db.js
│
├── controllers/ # Route logic handlers
│ └── authController.js
│ └── todoController.js
│
├── middlewares/ # Custom middleware (auth, error handling)
│ └── authMiddleware.js
│
│
├── models/ # Mongoose schemas
│ └── userSchema.js
│ └── todoSchema.js
│
├── routes/ # API route definitions
│ └── authRoutes.js
│ └── todoRoutes.js
│
│
├── .env # Environment variables
├── .gitignore
├── package.json
├── index.js # Entry point
Feel free to connect or reach out for collaboration, feedback, or just to say hi!
- 🌐 Portfolio: peyush-nuwal-portfolio.vercel.app
- 🐙 GitHub: github.com/peyush-nuwal
- 💼 LinkedIn: linkedin.com/in/peyush-nuwal
- ✉️ Email: piyushnawal19@gmail.com
- 🐦 Twitter/X: @Nuwal_Peyush