AquaTrack Backend is a RESTful API built using Node.js
and Express.js
. It allows users to easily track their water consumption, monitor progress towards their goals and set personal water recommendations.
- Installation
- Usage
- API Documentation
- Developers
- Routes
- Environment Variables
- Contributing
- Deployment
- Clone the repository:
git clone https://github.com/KristinaHranovska/node-rest-api cd node-rest-api
- Install dependencies:
npm install
- Create a
.env
file in the root directory and add your environment variables (see Environment Variables section). - Start the server:
The server will run on
npm run dev
http://localhost:3000
.
To interact with the API, you can use tools like Postman
or cURL
. Below are the available routes and their descriptions.
The API is documented using Swagger
. After starting the server, you can access the Swagger UI
at:
https://aqua-track-api.onrender.com/api-docs
- Full implementation of
/users
endpoint:- Creation of a public user
registration
endpoint. - Creation of a public user
login
endpoint. - Writing the
authorization
layer. - Creation of a private endpoint for receiving information about the current user.
- Creation of a private endpoint for updating data of an authorized user (name, email, gender, weight, active time for sports, daily water intake and avatar).
- Creation of a private endpoint for issuing a new pair of tokens (access and update).
- Creating a private endpoint for user
logout
. - Implementation of the image saving mechanism using
Cloudinary
. - Implementation of authorization through
Google
. - Creation of an endpoint for changing the user's password in case he forgot his password.
- Implementation of email verification using
.ejs
.
- Creation of a public user
- BackEnd -
/water
endpoint:- Creation of a private endpoint for adding a record of the volume of water consumed.
- Creation of a private endpoint for editing a record of the volume of water consumed.
- Creation of a private endpoint for deleting a record of the volume of water consumed.
- Creation of a private endpoint for receiving data on the user's water consumption per day.
- Creation of a private endpoint for receiving data on the user's water consumption for a month.
- Writing Swagger for the
/water
endpoint.
- Writing Swagger for
/users
endpoint.
-
User registration
POST /users/signup
Request body:
{
"email": "[email protected]",
"password": "password123",
"name": "John Doe"
}
Response:
{
"name": "John Doe",
"email": "[email protected]",
"message": "User registered successfully. Please check your email to verify your account."
}
-
User login
POST /users/signin
Request body:
{
"email": "[email protected]",
"password": "password123"
}
Response:
{
"email": "[email protected]",
"name": "John Doe",
"avatar": "https://res.cloudinary.com/dntbkzhtq/image/upload/v1719141998/AquaTrack/defaultAvatar.webp",
"dailyWaterNorm": 1.5,
"message": "Welcome back, John Doe to the AquaTrack!"
}
-
User logout
POST /users/logout
Request body:
{
"id": "667ea32bdcebb99c66d54321"
}
Response:
{
"message": "You have successfully exited"
}
- Verify user email
GET /users/verify/{verificationToken}
Response:
{
"message": "Verification successful. You can now access your account."
}
-
Refresh user access token
POST /users/refresh-tokens
Request body:
{
"refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY1YWQyZWRkMTEwYjBiOGRlZjQ4MTY1ZiIsImlhdCI6MTcwNTg0ODg3NSwiZXhwIjoxNzA2NDUzNjc1fQ.MONgGZKIUzqvq13OAlJvBctxJl1rt5OXMFQeIiZm2Aq"
}
Response:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY0YjFlMjVmNmIwYTJjY2I5NTU5MWVjNyIsImlhdCI6MTY4OTM3OTQyMywiZXhwIjoxNjg5NDYyMjIzfQ.hT2Ta6pBhDR1vOF7LjcKxofyASDPjcTZtFi9CESKIuA",
"refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY1YWQyZWRkMTEwYjBiOGRlZjQ4MTY1ZiIsImlhdCI6MTcwNTg0ODg3NSwiZXhwIjoxNzA2NDUzNjc1fQ.MONgGZKIUzqvq13OAlJvBctxJl1rt5OXMFQeIiZm2A"
}
-
Forgot password
POST /users/forgot
Request body:
{
"email": "[email protected]"
}
Response:
{
"message": "Password recovery email has been sent"
}
-
Reset user password
POST /users/reset
Request body:
{
"resetToken": "f7c564e8-7317-4c76-b5d2-2a0606b1e03a",
"password": "newPassword123"
}
Response:
{
"message": "Password has been reset successfully"
}
-
Google OAuth
GET /users/google
-
Google OAuth redirect
GET /users/google-redirect
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": "123",
"name": "John Doe",
"email": "[email protected]"
}
}
- Update user profile
PATCH /users/update
Request body:
{
"name": "John Doe",
"avatar": "https://res.cloudinary.com/dntbkzhtq/image/upload/v1719141998/AquaTrack/defaultAvatar.webp",
"gender": "male",
"weight": 70,
"dailyActivityTime": 3,
"dailyWaterNorm": 3
}
Response:
{
"_id": "60dcf3c65f45a500153e4b27",
"name": "John Doe",
"email": "[email protected]",
"gender": "male",
"weight": 70,
"dailyActivityTime": 3,
"dailyWaterNorm": 3,
"avatar": "https://res.cloudinary.com/dntbkzhtq/image/upload/v1719141998/AquaTrack/defaultAvatar.webp"
}
- Get user profile
GET /users/profile
Response:
{
"_id": "60d0fe4f5311236168a109ca",
"name": "John Doe",
"email": "[email protected]",
"avatar": "https://res.cloudinary.com/dntbkzhtq/image/upload/v1719141998/AquaTrack/defaultAvatar.webp",
"gender": "woman",
"weight": 0,
"dailyActivityTime": 0,
"dailyWaterNorm": 1.5
}
- Get happy endpoint
GET /users/happy
Response:
{
"count": 100,
"avatars": [
"https://res.cloudinary.com/dntbkzhtq/image/upload/v1719141998/AquaTrack/avatar.webp"
]
}
- Add water record
POST /water
Request body:
{
"amount": 0.4,
"date": "2024-07-10T10:17:20.886Z"
}
Response:
{
"newWaterRecord": {
"amount": 0.5,
"date": "2024-07-10T10:17:20.886Z",
"owner": "667c4a6aa00f3339fea36b1b",
"_id": "66855549f18f2eac7515fd00",
"createdAt": "2024-07-03T13:42:33.278Z",
"updatedAt": "2024-07-03T13:42:33.278Z"
},
"message": "Water record successfully added"
}
- Update water amount
PUT /water/:id
Request body:
{
"amount": 0.2,
"date": "2024-07-10T10:17:20.886Z"
}
Response:
{
"updatedRecord": {
"_id": "66855549f18f2eac7515fd00",
"amount": 0.2,
"date": "2024-07-10T10:17:20.886Z",
"owner": "667c4a6aa00f3339fea36b1b",
"createdAt": "2024-07-03T13:42:33.278Z",
"updatedAt": "2024-07-03T13:45:35.216Z"
},
"message": "Water record successfully updated"
}
- Delete water record
DELETE /water/:id
Request body:
{
"id": "66855549f18f2eac7515fd00"
}
Response:
{
"newWaterRecord": {
"amount": 0.3,
"date": "2024-06-28T11:35:59.880Z",
"owner": "4579687c17342250d5344321",
"_id": "667ea32bdcebb99c66d54321",
"createdAt": "2024-06-28T11:48:59.927Z",
"updatedAt": "2024-06-28T13:26:25.271Z"
},
"message": "Water record succesfully deleted"
}
- Get a daily water record
GET /water/daily/:date
Response:
{
"totalAmountForDay": 1.4,
"percentComplete": 75,
"records": [
{
"amount": 0.5,
"date": "2024-07-01T12:30:00Z",
"owner": "614f5e3e8c48d53a08a7efc6"
}
]
}
- Get a mouthly water record
GET /water/monthly/:year/:month
Response:
{
"totalWaterForMonth": 23.4,
"daysInMonth": [
{
"id": "67ca1234dceba44ac54b6",
"day": "2024-06-26",
"totalAmount": 1,
"percentComplete": 66.66
},
{
"id": "654ad234dceba44dab345",
"day": "2024-06-27",
"totalAmount": 0.75,
"percentComplete": 50
},
{
"id": "545bc32bdceba44d66d54",
"day": "2024-06-28",
"totalAmount": 1.2,
"percentComplete": 80
}
]
}
Create a .env
file in the root directory of your project and add the following environment variables:
DB_HOST=
PORT=
SENDGRID_API_KEY=
SECRET_KEY=
REFRESH_SECRET_KEY=
CLOUDINARY_CLOUD_NAME=
CLOUDINARY_API_KEY=
CLOUDINARY_API_SECRET=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
BASE_URL=
FRONTEND_URL=
Replace your_mongodb_connection_string
with your actual MongoDB connection string.
If you would like to contribute to this project, please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes.
- Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature-branch
). - Create a new Pull Request.
The code is deployed using render.com. You can access the deployed application at:
https://aqua-track-api.onrender.com/