A simple RESTful API for managing to-do items with user authentication, built using Node.js, Express, and MongoDB. This project supports creating, reading, updating, and deleting to-do items with secure JWT-based authentication.
https://roadmap.sh/projects/todo-list-api
- Introduction
- Features
- Technologies Used
- Installation
- Configuration
- Usage
- API Endpoints
- Examples
- Troubleshooting
- Contributors
- License
The Todo Management API allows users to manage their to-do items efficiently. It includes user authentication for secure access and supports CRUD operations with optional pagination for fetching to-do items. Built using modern web development practices, it ensures secure and scalable performance.
- User Authentication:
- Sign up and sign in with hashed passwords and JWT.
- Secure endpoints with token-based authentication.
- Todo Management:
- Add, update, retrieve, and delete to-do items.
- Support for paginated retrieval of items.
- Error Handling:
- Handles invalid requests, authentication errors, and server issues.
- Environment Configuration:
- Supports
.envfor customizable setups.
- Supports
- Node.js
- Express.js
- MongoDB with Mongoose ORM
- JWT for authentication
- bcrypt for password hashing
- dotenv for environment variable management
-
Clone the repository:
git clone https://github.com/yourusername/todo-management-api.git
-
Navigate to the project directory:
cd todo-management-api -
Install the dependencies:
npm install
-
Set up the environment variables by creating a
.envfile in the root directory and adding the following:MONGODB_URL=your-mongodb-connection-string SECRET_KEY=your-secret-key PORT=your-port
- MongoDB: Ensure you have a MongoDB instance running and configure the
MONGODB_URLin the.envfile. - JWT Secret: Set a secure secret key in the
SECRET_KEYfield for token signing. - Port: Specify the port in the
.envfile, or the server defaults to port3000.
-
Start the server:
npm start
-
The API will run at
http://localhost:3000(or the configured port).
-
Sign Up:
- URL:
/signup - Method:
POST - Body:
{ "name": "string", "email": "string", "password": "string" } - Response: Returns a user object and a JWT token.
- URL:
-
Sign In:
- URL:
/signin - Method:
GET - Body:
{ "email": "string", "password": "string" } - Response: Returns a user object and a JWT token.
- URL:
-
Add Todo:
- URL:
/addTodo - Method:
POST - Headers:
Authorization: Bearer <token> - Body:
{ "title": "string", "description": "string" } - Response: Returns the created to-do item.
- URL:
-
Get Todos:
- URL:
/getTodos/todos?page=number&limit=number - Method:
GET - Headers:
Authorization: Bearer <token> - Response: Returns a paginated list of to-do items.
- URL:
-
Update Todo:
- URL:
/:id - Method:
POST - Headers:
Authorization: Bearer <token> - Body:
{ "title": "string", "description": "string" } - Response: Returns the updated to-do item.
- URL:
-
Delete Todo:
- URL:
/:id - Method:
DELETE - Headers:
Authorization: Bearer <token> - Response: Returns a success message.
- URL:
-
Create a new to-do:
curl -X POST http://localhost:3000/addTodo \ -H "Authorization: Bearer <token>" \ -H "Content-Type: application/json" \ -d '{"title": "Sample Todo", "description": "This is a test to-do"}'
-
Get all to-dos:
curl -X GET http://localhost:3000/getTodos/todos?page=1&limit=5 \ -H "Authorization: Bearer <token>"
- MongoDB Connection Issues: Ensure the MongoDB URI in the
.envfile is correct and that the MongoDB service is running. - Invalid Token: Make sure to pass a valid JWT in the
Authorizationheader. - Port Already in Use: Change the port in the
.envfile or terminate the process using the current port.
This project is licensed under the MIT License. See the LICENSE file for details.