- Clone the repository:
git clone [email protected]:matenka-andrii/rest-api-nodejs-mongodb.git
- Change into the project directory:
cd rest-api-nodejs-mongodb
- Install dependencies:
npm install
- Create a
.env
file in the root directory of the project with the following contents:
MONGODB_URL=mongodb:https://mongouser:mongopass@localhost:27017
JWT_SECRET=jwt_secret
PORT=3000
REDIS_URI=redis:https://:your_password@localhost:6379
REDIS_PASS=your_password
- Start the Docker containers (Note: You must have the Docker Desktop application installed on your system):
docker compose up -d
- Start the server:
npm run start:dev
After completing the above steps, the server should be running and accessible at https://localhost:3000.
This section provides detailed information about the available endpoints that can be accessed using Postman.
- Method: POST
- Endpoint: /users
- Body: JSON object containing the user's details. Fields required are as follows:
{
"firstName": "YourFirstName",
"lastName": "YourLastName",
"email": "[email protected]",
"password": "YourSecurePassword"
}
Authenticate User
- Method: POST
- Endpoint: /login
- Body: JSON object containing the user's credentials. Required fields are as follows:
{
"email": "[email protected]",
"password": "YourSecurePassword"
}
- Description: This endpoint authenticates a user by their email and password. Upon successful authentication, it returns a token which should be used for subsequent requests requiring authentication.
- After receiving the token, it must be included in the header of all authenticated requests as a Bearer token. This is critical for accessing any endpoints that require authorization.
- To do this, set the
Authorization
header of your request in Postman as follows:Bearer <your_token_here>
. - Replace
<your_token_here>
with the actual token received from the /login endpoint.
- Method: POST
- Endpoint: /categories
- Body: JSON object containing the category's details. Fields required are as follows:
{
"name": "YourCategoryName",
}
- Method: GET
- Endpoint: /categories
- Authorization:
Bearer <your_token_here>
- Returns: This endpoint returns a JSON array of categories. Each category object contains an ID and a name attribute.
[
{
"id": "65ca211f2591239c220243d1",
"name": "Technology"
},
{
"id": "65ca211f2591239c220243d2",
"name": "Science"
}
]
- Description: Use this endpoint to fetch a list of all categories available in the system.
- Method: GET
- Endpoint: /categories/:id
- Authorization:
Bearer <your_token_here>
- Params:
{
params: {
id: "created_category_id",
}
}
-
Update category:
PUT /categories/:id
-
Delete category:
DELETE /categories/:id
-
Create product:
POST /products
-
Get products:
GET /products
-
Get product by id:
GET /products/:id
-
Get products by category:
GET /products/category/:categoryId
-
Update product:
PUT /products/:id
-
Delete product:
DELETE /products/:id