To create a project, simply run:
Clone the repo:
git clone https://github.com/caonam030402/ECommerce-RestApi-NodeJs.git
Install the dependencies:
npm install
Running locally:
npm run dev
Linting:
# run ESLint
npm run lint
# fix ESLint errors
npm run lint:fix
# run prettier
npm run prettier
# fix prettier errors
npm run prettier:fix
The environment variables can be found and modified in the .env
file. They come with these default values:
URL_MONGOOSE =
ACCESS_TOKEN_SECRET =
REFRESH_TOKEN_SECRET =
ACCESS_TOKEN_EXPIRES_IN =
REFRESH_TOKEN_EXPIRES_IN =
PORT =
# KEY PAYMENT VNPAY
vnp_TmnCode =
vnp_HashSecret =
vnp_Url =
vnp_ReturnUrl =
Method: POST body
{
"email": "caonam81@gmail.com",
"password": "caonam123"
}
Response
{
"message": "Đăng kí thành công",
"data": {
"user": {
"email": "caonam81@gmail.com",
"roles": [],
"_id": "6499a8a72504fe6e4cf9b8fc",
"createdAt": "2023-06-26T15:03:03.964Z",
"updatedAt": "2023-06-26T15:03:03.964Z",
"__v": 0
},
"access_token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NDk5YThhNzI1MDRmZTZlNGNmOWI4ZmMiLCJpYXQiOjE2ODc3OTE3ODQsImV4cCI6MTY4Nzc5MTgxNH0.Uk_tVLpk-7eNqTUKzz6VIQuhfgc69-Iyy9VkQbRda5A",
"refresh_token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NDk5YThhNzI1MDRmZTZlNGNmOWI4ZmMiLCJpYXQiOjE2ODc3OTE3ODQsImV4cCI6MTY4NzgyNzc4NH0.G1FSWF0BVQLJH73m-Zh6cOylJBxdx3VAjYM4jfKcLTg",
"expires": "1h",
"expires_refresh_token": "10h"
}
}
Method: POST body
{
"email": "caonam81@gmail.com",
"password": "caonam123"
}
Response
{
"message": "Đăng nhập thành công",
"data": {
"access_token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NDk5YTEzMzEyZjMxZTJlYzQwMDJiY2IiLCJpYXQiOjE2ODc3OTE2OTMsImV4cCI6MTY4Nzc5MTcyM30.-xEMR2rF7SXZB0x4bD-qaYwNjvwQNVoSEa6FF6DJsxA",
"expires": "2h",
"refresh_token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NDk5YTEzMzEyZjMxZTJlYzQwMDJiY2IiLCJpYXQiOjE2ODc3OTE2OTMsImV4cCI6MTY4NzgyNzY5M30.lLIpGK8U3KPlwU4ngaqUxNw7sq3zMVWKqMyJ6Dz3Ln8",
"expires_refresh_token": "10h",
"user": {
"_id": "6499a13312f31e2ec4002bcb",
"email": "caonam81@gmail.com",
"roles": [],
"createdAt": "2023-06-26T14:31:15.457Z",
"updatedAt": "2023-06-26T14:31:15.457Z",
"__v": 0
}
}
}
Method: POST header
{
"access_token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NDk5YTEzMzEyZjMxZTJlYzQwMDJiY2IiLCJpYXQiOjE2ODc3OTE2OTMsImV4cCI6MTY4Nzc5MTcyM30.-xEMR2rF7SXZB0x4bD-qaYwNjvwQNVoSEa6FF6DJsxA"
}
Response
{
"message": "Đăng xuất thành công"
}
Method: GET header
{
"access_token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NDk5YTEzMzEyZjMxZTJlYzQwMDJiY2IiLCJpYXQiOjE2ODc3OTE2OTMsImV4cCI6MTY4Nzc5MTcyM30.-xEMR2rF7SXZB0x4bD-qaYwNjvwQNVoSEa6FF6DJsxA"
}
Response
{
"message": "Lấy người dùng thành công",
"data": {
"_id": "6499a13312f31e2ec4002bcb",
"email": "caonam811@gmail.com",
"roles": [],
"createdAt": "2023-06-26T14:31:15.457Z",
"updatedAt": "2023-06-26T14:31:15.457Z",
"__v": 0
}
}
Method: PUT body
{
"name": "caonam",
"password": "caonam",
"address": "Da Nang, Viet Nam",
"phoneNumber": "0382714916",
"new_password": "Naonam32323@"
}
Response
{
"message": "Cập nhập người dùng thành công",
"data": {
"_id": "6499a13312f31e2ec4002bcb",
"email": "caonam811@gmail.com",
"address": "Da Nang, Viet Nam",
"roles": [],
"createdAt": "2023-06-26T14:31:15.457Z",
"updatedAt": "2023-06-26T15:14:07.819Z",
"__v": 0,
"name": "caonam"
}
}
Method: POST form-data
Response
{
"message": "Upload ảnh đại diện thành công",
"data": "avatar-1687793282939-639104443.png"
}
Method: GET
Response
{
"message": "Lấy địa chỉ thành công",
"data": [
{
"_id": "6482952f4744efbce8dc2e4f",
"Id": "01",
"Name": "Thành phố Hà Nội",
"Districts": [
{
"_id": "6499aee4ec3124d01fa8d8fb",
"Id": "001",
"Name": "Quận Ba Đình",
"Wards": [
{
"Id": "00031",
"Name": "Phường Giảng Võ",
"Level": "Phường"
},
{
"Id": "00034",
"Name": "Phường Thành Công",
"Level": "Phường"
}
]
}
]
}
]
}
For example: products?page=1&limit=30
Method: GET
Query Params:
page
: number. Number of pages. Default is 1limit
: number. Number of products per page. Default is 30order
: 'desc' || 'asc'. Sort by order. Default is 'desc'sort_by
: 'createdAt' || 'view' || 'sold' || 'price'. Sort by field. Default is 'createdAt'.category
: categoryId. Filter products by categoryexclude
: productId. Exclude certain productsrating_filter
: number. Filter products with stars greater than or equal to rating_filterprice_max
: number. Highest priceprice_min
: number. Lowest pricename
: string. Product Name (Note: Vietnamese product name must be filled with punctuation marks)
Response
{
"message": "Lấy các sản phẩm thành công",
"data": {
"products": [],
"pagination": {
"page": 1,
"limit": 30,
"page_size": 2
}
}
}
Method: POST body
[
{
"_id": "60afb2426ef5b902180aacb9",
"images": ["url_1", "url_2", "url_3", "url_4", "url_5"],
"price": 2590000,
"price_before_discount": 3490000,
"quantity": 73,
"sold": 6800,
"view": 17527,
"name": "Điện thoại iphone 14",
"category": {
"_id": "60afafe76ef5b902180aacb5",
"name": "Điện thoại"
},
"image": "url_1"
}
]
Response
{
"message": "Thêm sản phẩm thành công thành công",
"data": [
{
"_id": "60afb2426ef5b902180aacb9",
"images": ["url_1", "url_2", "url_3", "url_4", "url_5"],
"price": 2590000,
"price_before_discount": 3490000,
"quantity": 73,
"sold": 6800,
"view": 17527,
"name": "Điện thoại iphone 14",
"category": {
"_id": "60afafe76ef5b902180aacb5",
"name": "Điện thoại"
},
"image": "url_1"
}
]
}
Method: PUT body
{
"_id": "60afb2426ef5b902180aacb9",
"images": ["url_1", "url_2", "url_3", "url_4", "url_5"],
"price": 2590000,
"price_before_discount": 3490000,
"quantity": 73,
"sold": 6800,
"view": 17527,
"name": "Điện thoại iphone 14",
"category": {
"_id": "60afafe76ef5b902180aacb5",
"name": "Điện thoại"
},
"image": "url_1"
}
Response
{
"message": "Cập nhập sản phẩm công",
"data": {
"_id": "60afb2426ef5b902180aacb9",
"images": ["url_1", "url_2", "url_3", "url_4", "url_5"],
"price": 2590000,
"price_before_discount": 3490000,
"quantity": 73,
"sold": 6800,
"view": 17527,
"name": "Điện thoại iphone 14",
"category": {
"_id": "60afafe76ef5b902180aacb5",
"name": "Điện thoại"
},
"image": "url_1"
}
}
For example: v1/products/delete-product/6474d5ee11873c33396abb62
Method: DELETE
Response
{
"success": true,
"message": "Xóa sản phẩm thành công"
}
For example: v1/products/6460a53fcf48dd81cc4c635a
Method: GET
Response
{
"message": "Lấy sản phẩm thành công",
"data": {
"_id": "60afb2426ef5b902180aacb9",
"images": ["url_1", "url_2", "url_3", "url_4", "url_5"],
"price": 2590000,
"price_before_discount": 3490000,
"quantity": 73,
"sold": 6800,
"view": 17527,
"name": "Điện thoại iphone 14",
"category": {
"_id": "60afafe76ef5b902180aacb5",
"name": "Điện thoại"
},
"image": "url_1"
}
}
Method: POST body
[
{
"_id": "60aba4e24efcc70f8892e1c6",
"name": "Áo thun"
},
{
"_id": "60afacca6ef5b902180aacaf",
"name": "Đồng hồ"
},
{
"_id": "60afafe76ef5b902180aacb5",
"name": "Điện thoại"
}
]
Response
{
"message": "Thêm danh mục thành công",
"data": [
{
"_id": "60aba4e24efcc70f8892e1c6",
"name": "Áo thun"
},
{
"_id": "60afacca6ef5b902180aacaf",
"name": "Đồng hồ"
},
{
"_id": "60afafe76ef5b902180aacb5",
"name": "Điện thoại"
}
]
}
Method: GET
Response
{
"message": "Thêm danh mục thành công",
"data": [
{
"_id": "60aba4e24efcc70f8892e1c6",
"name": "Áo thun",
"createdAt": "2023-05-20T10:08:07.483Z",
"updatedAt": "2023-05-20T10:08:07.483Z",
"__v": 0
},
{
"_id": "60afacca6ef5b902180aacaf",
"name": "Đồng hồ",
"createdAt": "2023-05-20T10:08:07.483Z",
"updatedAt": "2023-05-20T10:08:07.483Z",
"__v": 0
},
{
"_id": "60afafe76ef5b902180aacb5",
"name": "Điện thoại",
"createdAt": "2023-05-20T10:08:07.483Z",
"updatedAt": "2023-05-20T10:08:07.483Z",
"__v": 0
}
]
}
Method: POST body
[{ "purchase_id": "6464f0b4b2ffb6c7f8b470d8" }, { "purchase_id": "6464f6a1e244cb8190cc4c93" }]
Response
{
"message": "Mua thành công",
"data": {}
}
Method: POST body
{ "status": -1, "buy_count": 2 }
Method: POST body
[{ "purchase_id": "6464f0b4b2ffb6c7f8b470d8" }, { "purchase_id": "6464f6a1e244cb8190cc4c93" }]
Response
{
"message": "Mua thành công",
"data": {}
}
Method: POST body
{
"product_id": "60afaf286ef5b902180aacb3",
"buy_count": 2
}
Response
{
"message": "Thêm sản phẩm vào giỏ hàng thành công",
"data": {
"buy_count": 2,
"price": 300000,
"price_before_discount": 450000,
"status": -1,
"user": "6499a13312f31e2ec4002bcb",
"product": {
"message": "Lấy sản phẩm thành công",
"data": {
"_id": "60afb2426ef5b902180aacb9",
"images": ["url_1", "url_2", "url_3", "url_4", "url_5"],
"price": 2590000,
"price_before_discount": 3490000,
"quantity": 73,
"sold": 6800,
"view": 17527,
"name": "Điện thoại iphone 14",
"category": {
"_id": "60afafe76ef5b902180aacb5",
"name": "Điện thoại"
},
"image": "url_1"
}
},
"_id": "6499b4deec3124d01fa8dbd4",
"createdAt": "2023-06-26T15:55:10.600Z",
"updatedAt": "2023-06-26T15:55:10.600Z",
"__v": 0
}
}
Method: DELETE body
["6464f025b2ffb6c7f8b4706e", "6464f027b2ffb6c7f8b47078"]
Response
{
"message": "Hủy đơn thành công"
}
For example: user/purchase/?status=1
Method: GET
Query Params:
status
: Order Status
Info status
:
- -1: Product is in cart
- 0: All products
- 1: The product is waiting for confirmation from the shop owner
- 2: Products are being picked up
- 3: Products are in transit
- 4: The product has been delivered
- 5: Product has been canceled
Method: GET
Response
{
"message": "Lấy thông tin tổng quan thành công",
"data": {
"totalAmoutSold": {
"_id": null,
"total": 16104000
},
"totalProduct": {
"_id": null,
"total": 118005
},
"totalProductSold": {
"_id": null,
"total": 16
},
"totalUser": {
"_id": null,
"total": 42
}
}
}
Method: POST body
{
"amount": 20000,
"bankCode": "",
"language": "vn"
}
Response
{
"message": "Lấy URL thành công",
"data": "https://sandbox.vnpayment.vn/paymentv2/vpcpay.html?vnp_Amount=2000000&vnp_Command=pay&vnp_CreateDate=20230626210008&vnp_CurrCode=VND&vnp_IpAddr=%3A%3A1&vnp_Locale=vn&vnp_OrderInfo=Thanh+toan+cho+ma+GD%3A26210008&vnp_OrderType=other&vnp_ReturnUrl=http%3A%2F%2Flocalhost%3A8888%2Forder%2Fvnpay_return&vnp_TmnCode=QV0U7DIY&vnp_TxnRef=26210008&vnp_Version=2.1.0&vnp_SecureHash=ff1c27c4b8b1934b826004020828341248674b38a27fa6fa4251320d25059253bb0b65105b64134576bd219aadad46d42e29908947ffb6761683052e10213a71"
}
For example: /v1/images/image-1684460733977-638357616.png
Method: GET
Method: POST body
{
"refresh_token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NDkxMWMwYzViYTYzYjQ4MWYxZWRlOTAiLCJpYXQiOjE2ODcyNTY2MDQsImV4cCI6MTY4NzI2MDIwNH0.NU1iKylj_XmkPry9ziNM9DzbyIf3KF_kjMMmzU8AgkU"
}