This project created for learning purpose of how to make an API using nodejs and express.js. Feel free to use this project for reference. or not.
- Node.js (tested on v14.15.3)
- NPM (tested on v6.14.9)
- Yarn (tested on v1.22.5)
- MySQL 5 (tested on v5.7.37)
Clone and install dependencies
> git clone https://github.com/reyusfa/pos-api.git
> cd pos-api
> yarn install
- Install MySQL.
- Create new database.
- Import file
point_of_sale.sql
to your database. - Edit file .env to configure Server & MySQL connection.
SERVER_PORT=3001
SERVER_HOST=127.0.0.1
DB_HOST=127.0.0.1
DB_NAME=point_of_sale
DB_USERNAME=root
DB_PASSWORD=
PUBLIC_IMAGES=http://localhost:3001
SECRET_KEY=SECRET_KEY
> yarn start
now listening on http://127.0.0.1:3001
now connected to database...
├── index.js
├── node_modules
├── package.json
├── point_of_sale.sql
├── public
│ └── images
├── README.md
└── src
├── config
│ └── mysql.js
├── controller
│ ├── categories.js
│ ├── orders.js
│ └── products.js
├── helper
│ └── index.js
├── middleware
│ └── files.js
├── models
│ ├── categories.js
│ ├── orders.js
│ └── products.js
└── routes
├── categories.js
├── index.js
├── orders.js
└── products.js
POST
/login
Content-Type: application/json [OR] application/x-www-form-urlencoded
Authorization: TOKEN
{
"username": "admin",
"password": "admin"
}
{
"status": 200,
"data": {
"token": "TOKEN",
"id": 1,
"role_id": 1,
"name": "Admin"
}
}
POST
/register
Content-Type: multipart/form-data
Authorization: TOKEN
{
"username": "badmin",
"password": "badmin",
"email": "badmin@email.com",
"name": "Badmin",
"image": "", //optional
"role_id": 1
}
{
"status": 200,
"data": {
"id": 23,
"username": "badmin",
"email": "badmin@email.com",
"name": "Badmin",
"image": "",
"role_id": 1
}
}
Endpoint list
GET
/products
GET
/products/:id
POST
/products
PUT
/products/:id
DELETE
/products/:id
GET
/products
{
"code": 200,
"data": [
{
"id": 1,
"name": "Juice",
"price": 2000,
"category_id": 1,
"description": "Fresh Juice",
"image": "public/images/image-1579661507990.jpeg",
"created_at": "2020-01-22T02:51:48.000Z",
"updated_at": "2020-01-22T02:51:48.000Z"
},
{
"id": 2,
"name": "Tea",
"price": 2000,
"category_id": 1,
"description": "Fresh Tea",
"image": "public/images/image-1579661516758.jpeg",
"created_at": "2020-01-22T02:51:56.000Z",
"updated_at": "2020-01-22T02:51:56.000Z"
}
]
}
Sorting, Filtering, & Pagination
Supported query parameters:
?sort=name.desc
?filter[name]=
?limit=80
?limit=10&page=2
?limit=10&offset=30
GET
/products/:id
{
"code": 200,
"data": {
"id": 1,
"name": "Juice",
"price": 2000,
"category_id": 1,
"description": "Fresh Juice",
"image": "public/images/image-1579661507990.jpeg",
"created_at": "2020-01-22T02:51:48.000Z",
"updated_at": "2020-01-22T02:51:48.000Z"
}
}
POST
/products
Content-Type: multipart/form-data
Authorization: TOKEN
{
"name": "Orange Juice",
"price": 3000,
"category_id": 1,
"image": "", //optional
"description": "Fresh Orange Juice"
}
PUT
/products/:id
Content-Type: multipart/form-data
Authorization: TOKEN
{
"price": 3000,
}
DELETE
/products/:id
Content-Type: application/json [OR] application/x-www-form-urlencoded
Authorization: TOKEN
Endpoint list:
GET
/categories
GET
/categories/:id
POST
/categories
PUT
/categories/:id
DELETE
/categories/:id
GET
/categories
{
"code": 200,
"data": [
{
"id": 1,
"name": "Drink",
"created_at": "2020-01-22T02:51:48.000Z",
"updated_at": "2020-01-22T02:51:48.000Z"
},
{
"id": 2,
"name": "Meal",
"created_at": "2020-01-22T02:51:56.000Z",
"updated_at": "2020-01-22T02:51:56.000Z"
}
]
}
GET
/categories/:id
{
"code": 200,
"data": {
"id": 1,
"name": "Drink",
"created_at": "2020-01-22T02:51:48.000Z",
"updated_at": "2020-01-22T02:51:48.000Z"
}
}
POST
/categories
Content-Type: application/json [OR] application/x-www-form-urlencoded
Authorization: TOKEN
{
"name": "Snack"
}
PUT
/categories/:id
Content-Type: application/json [OR] application/x-www-form-urlencoded
Authorization: TOKEN
{
"name": "Coklat"
}
DELETE
/categories/:id
Content-Type: application/json [OR] application/x-www-form-urlencoded
Authorization: TOKEN
Endpoint list:
GET
/orders
GET
/orders/:id
GET
/orders/:id/items
GET
/orders/:id/items/:id_item
POST
/orders
DELETE
/orders/:id
GET
/orders
{
"code": 200,
"data": [
{
"id": 1,
"reference": "67478591",
"user_id": 1,
"total": 50000,
"created_at": "2020-01-22T13:51:33.000Z",
"updated_at": "2020-01-22T13:51:33.000Z"
},
{
"id": 1,
"reference": "34751923",
"user_id": 1,
"total": 20000,
"created_at": "2020-01-22T13:51:33.000Z",
"updated_at": "2020-01-22T13:51:33.000Z"
}
]
}
GET
/orders/:id
{
"code": 200,
"data": {
"id": 1,
"reference": "67478591",
"user_id": 1,
"total": 50000,
"created_at": "2020-01-22T02:51:48.000Z",
"updated_at": "2020-01-22T02:51:48.000Z"
}
}
GET
/orders/:id/items
GET
/orders/:id/items/:id_item
POST
/orders
Content-Type: application/json [OR] application/x-www-form-urlencoded
Authorization: TOKEN
{
"user_id": 1,
"orders": [
{
"product_id": 1,
"quantity": 1
},
{
"product_id": 2,
"quantity": 2
}
]
}
DELETE
/orders/:id
Content-Type: application/json [OR] application/x-www-form-urlencoded
Authorization: TOKEN
GET
/users
GET
/users/:id
PUT
/users/:id
DELETE
/users/:id