This is my work assigned by Route-Academy during learning backend web development.
The provided code is a Node.js and Express.js server implementation that exposes APIs for managing users and products stored in a MySQL database. The APIs allow operations such as retrieving users and products, adding new entries, updating existing entries, and deleting entries (CRUD).
The code in this module runs with the main server file index.js
in the root directory.
Install dependencies:
npm install
To run the server:
node index.js
The server will start running on http://localhost:3000
.
Retrieves information about all users.
- URL:
/users
- Method:
GET
Adds a new user to the database.
- URL:
/users
- Method:
POST
Request Body:
{
"name": "Ahmed",
"email": "hi@ahmed.com",
"password": "908jkha2",
"age": 25
}
Updates an existing user in the database.
- URL:
/users
- Method:
PUT
Request Body:
{
"id": 2,
"name": "Mostafa",
"email": "hi@mostafa.com",
"password": "jkji982"
}
Deletes a user from the database.
- URL:
/users
- Method:
DELETE
Request Body:
{
"id": 20
}
Retrieves users whose name starts with "a" and age is less than 30.
- URL:
/users/a30
- Method:
GET
Retrieves users whose IDs match the provided list.
- URL:
/users/ids
- Method:
GET
Request Body:
[1, 2, 6, 7, 8]
Retrieves information about all products.
- URL:
/products
- Method:
GET
Adds a new product to the database.
- URL:
/products
- Method:
POST
Request Body:
{
"name": "Samsung",
"price": 1400,
"desc": "Not Bad",
"createdBy": "Mostafa",
"userId": 21
}
Updates an existing product in the database.
- URL:
/products
- Method:
PUT
Request Body:
{
"id": 10,
"name": "iPhone",
"price": "1400",
"desc": "Not Bad",
"createdBy": "Mostafa",
"userId": 20
}
Deletes a product from the database.
- URL:
/products
- Method:
DELETE
Request Body:
{
"id": 1,
"userId": 20
}
Retrieves products with prices greater than 3000.
- URL:
/products/p3000
- Method:
GET
Checks if an email already exists in the database before adding a new user. It ensures that no duplicate emails are registered.
Checks if a user with a given ID exists in the database before updating or deleting a user. It prevents updating or deleting non-existent users.
Checks if a product with a given name and user ID already exists in the database before adding a new product. It ensures that no duplicate products are added for the same user.
Checks if a product with a given ID and user ID exists in the database before updating or deleting a product. It prevents updating or deleting non-existent products or unauthorized access to products.
- GitHub - IMostafaR
- Linkedin - @imostafarh