Develop a backend RESTful API using Node.js for managing tasks. Users should be able to create, retrieve, update, and delete tasks.
Backend Deploy: https://task-management-api-ukl3.onrender.com/
- POST user/register: Add a new user register.
- POST user/login: user Login.
- POST task/tasks: Add a new task.
- GET task/tasks: Retrieve a list of all tasks.
- GET task/tasks/:id: Retrieve a specific task by ID.
- PUT task/tasks/:id: Update a specific task by ID.
- DELETE task/tasks/:id: Delete a specific task by ID.
Each task should have the following properties:
- ID: A unique identifier for the task.
- Title: A brief title describing the task.
- Description: Additional details about the task.
- Creation Date: The date and time when the task was created.
- Status: The status of the task (e.g., pending, completed).
Before you begin, ensure you have the following installed on your machine:
-
Clone the Repository:
git clone <(https://github.com/himanshu60/Task-Management-API)>
-
Install Dependencies:
Navigate to the project directory and install the required packages using npm:
cd Task-Management-API npm install
-
Set Environment Variables:
Create a
.env
file in the project root and add the following environment variables:PORT=8080 URL="mongoDB url" KEY=yourkey
Adjust the
PORT
andURL
variables as needed. -
Run the API:
Start the API server using npm:
node index.js
The API will be running at
http://localhost:8080
.
- Endpoint:
POST /tasks
- Request Body:
{
"title": "Vacation Planning",
"description": "Plan a summer vacation destination",
"status": "Pending"
}
Create a new task with the provided title, description, and status (pending/completed).
-
Endpoint:
GET /tasks
Retrieve a list of all tasks.
-
Endpoint:
GET /tasks/:id
Retrieve a specific task by its ID.
-
Endpoint:
PUT /tasks/:id
-
Request Body:
{ "title": "Updated Task Title", "description": "Updated Task Description", "status": "completed" }
Update a specific task by its ID with the provided data.
-
Endpoint:
DELETE /tasks/:id
Delete a specific task by its ID.
To authenticate protected endpoints, you need to include a JWT token in the Authorization header of your requests. Follow these steps to authenticate:
-
Obtain a JWT Token
Make a POST request to the
/login
endpoint with valid credentials to obtain a JWT token:-
Endpoint:
POST /login
-
Request Body:
{ "email": "himanshu@gmail.com", "password": "himanshu" }
If the credentials are correct, you will receive a JSON response containing the JWT token:
{ "token": "your-jwt-token" }
-
-
Authenticate Protected Endpoints
Include the obtained JWT token in the Authorization header of your requests to access protected endpoints:
-
Endpoint: Any protected endpoint (e.g.,
/tasks
,/tasks/:id
) -
Request Headers:
Authorization: jwt-token
Replace
your-jwt-token
with the token obtained in the previous step.
If the token is valid, the protected endpoint will respond with the appropriate data. If the token is invalid or expired, you will receive a 401 Unauthorized response.
-
The API handles errors gracefully and returns appropriate HTTP status codes and error messages in case of failures. The responses include clear error messages to assist developers in diagnosing issues.
Feel free to explore the API endpoints using tools like Postman or integrate them into your applications. If you have any questions or encounter issues, please don't hesitate to reach out. Happy coding!
The Express Rate Limit Middleware is a powerful tool that helps you control the rate at which clients can make requests to your server. It prevents abuse and ensures the stability and availability of your services by limiting the number of requests from a client within a specified time window.
- Rate Limiting: Restrict clients from making too many requests in a short period.
- Configurable: Easily adjust the rate limit settings to suit your application's needs.
- IP-Based Limitation: Limit requests based on the client's IP address.
- Custom Messages: Send custom error messages when rate limits are exceeded.
To get started with the Express Rate Limit Middleware, install it as a dependency in your Node.js project:
npm install express-rate-limit
Use the middleware in your Express application as shown below:
const rateLimit = require('express-rate-limit');
// Create a rate limiter with your desired configuration
const Limiter = rateLimit({
windowMs: 60 * 60 * 1000, // 1 hour
max: 10, // Max 10 requests per IP per hour
message:
"Too many attempts from this IP. Please wait for 1 hour and then try again.",
});
// Apply the limiter to specific routes or globally as needed
app.use('/api/sensitive', Limiter);
// Your API routes and other middleware go here
You can customize the rate limit by modifying the windowMs
and max
options:
- windowMs: The time window in milliseconds during which requests are counted. In this example, it's set to 1 hour (3600000 milliseconds).
- max: The maximum number of requests allowed per IP address within the specified time window.