├── .gitignore
├── package.json
├── db.json
├── index.js
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | /node_modules
2 | .env
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "json-server",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "keywords": [],
10 | "author": "",
11 | "license": "ISC",
12 | "dependencies": {
13 | "body-parser": "^1.19.0",
14 | "cors": "^2.8.5",
15 | "express": "^4.17.1",
16 | "lowdb": "^1.0.0",
17 | "nanoid": "^3.1.10"
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/db.json:
--------------------------------------------------------------------------------
1 | {
2 | "products": [
3 | {
4 | "name": "Test product 3 (test edit)",
5 | "price": "5000000",
6 | "description": "product 3",
7 | "id": "xWbk1Ck9-ba7uQZZiFxpL"
8 | },
9 | {
10 | "name": "5645645",
11 | "price": "645645",
12 | "description": "65466546",
13 | "id": "rovKMQRGxpAUUQet_dE7W"
14 | },
15 | {
16 | "name": "ádasd",
17 | "price": "234234",
18 | "description": "sdsadas",
19 | "id": "7A3folJnaRJJzsaHWAZ81"
20 | },
21 | {
22 | "name": "Iphone 12",
23 | "price": "25000000",
24 | "description": "Thông tin chi tiết sản phẩm Iphone 12",
25 | "id": "oPSzE11HGgfcH0LJ4aSfd"
26 | }
27 | ],
28 | "users": [
29 | {
30 | "id": "5YsFNN42B14VYHhpDyizn",
31 | "name": "Administrator",
32 | "email": "admin@gmail.com",
33 | "password": 123456
34 | }
35 | ]
36 | }
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | const express = require("express")
2 | const cors = require("cors")
3 | const lowDb = require("lowdb")
4 | const FileSync = require("lowdb/adapters/FileSync")
5 | const bodyParser = require("body-parser")
6 | const { nanoid } = require("nanoid")
7 | const db = lowDb(new FileSync('db.json'))
8 | const fs = require('fs');
9 | let rawdata = fs.readFileSync('db.json');
10 | let routes = JSON.parse(rawdata);
11 |
12 | const PORT = 8000;
13 |
14 | db.defaults({ products: [] }).write()
15 |
16 | const app = express()
17 |
18 | const modules = Object.keys(routes)
19 |
20 | app.use(cors())
21 | app.use(bodyParser.json())
22 |
23 | app.get('/', (req, res) => {
24 | res.send(`
SUNTECH DEMO API
`)
25 | })
26 |
27 | app.post('/api/login', (req, res) => {
28 | const user = db.get('users').find({ email: req.body.email, password: req.body.password }).value()
29 |
30 | if (!user) {
31 | res.json({ success: false })
32 | } else {
33 | res.json({
34 | user: {
35 | id: user.id,
36 | name: user.name,
37 | email: user.email,
38 | },
39 | token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'
40 | })
41 | }
42 | })
43 |
44 | modules.forEach(moduleName => {
45 | app.get(`/api/${moduleName}`, (req, res) => {
46 | const data = db.get(`${moduleName}`).value()
47 | return res.json(data)
48 | })
49 |
50 | app.get(`/api/${moduleName}/:id`, (req, res) => {
51 | const data = db.get(`${moduleName}`).find({ id: req.params.id })
52 | return res.json(data)
53 | })
54 |
55 | app.post(`/api/${moduleName}`, (req, res) => {
56 | const note = req.body
57 | db.get(`${moduleName}`).push({
58 | ...note, id: nanoid()
59 | }).write()
60 |
61 | res.json({ success: true })
62 | })
63 |
64 | app.put(`/api/${moduleName}/:id`, (req, res) => {
65 | const note = req.body
66 | db.get(`${moduleName}`).find({ id: req.params.id }).assign(note).write();
67 |
68 | res.json({ success: true })
69 | })
70 |
71 | app.delete(`/api/${moduleName}/:id`, (req, res) => {
72 | db.get(`${moduleName}`).remove({ id: req.params.id }).write();
73 | res.json({ success: true })
74 | })
75 | })
76 |
77 |
78 | app.listen(PORT, ()=> {
79 | console.log(`Backend is running on http://localhost:${PORT}`)
80 | })
81 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## SUNTECH SIMPLE API
2 | - This is the source make simple API for all beginner Frontend Developer
3 | - It's includes
4 | - Lowdb
5 | - ExpressJS Example
6 | - Author: `Khôi Pham`
7 | - Bài viết hướng dẫn: https://suntech.edu.vn/api-danh-cho-frontend-developer.sunpost.html
8 | - Video hướng dẫn: https://youtu.be/oiDx9Y6zUts
9 |
10 | ## How to Setup
11 | - Step1: Install NodeJs (https://nodejs.org/en/)
12 | - Step2: Run command ```npm install```
13 | - Step3: Start server with command: ```node index.js```
14 | - Run on local: http://localhost:8000/
15 | - Step4: Install Postman for test API: https://www.postman.com/downloads/
16 |
17 | ## API infomation
18 | `baseUrl`: `http://localhost:8000`
19 | ### 1. List Product
20 |
21 | `GET`: `{baseUrl}/api/products`
22 |
23 | `Parameters`: No
24 | `Request body`: No
25 | `Response`:
26 | ```
27 | [
28 | .....
29 | {
30 | "name": "Test product 3 (test edit)",
31 | "price": "5000000",
32 | "description": "product 3",
33 | "id": "xWbk1Ck9-ba7uQZZiFxpL"
34 | }
35 | .....
36 | ]
37 | ```
38 |
39 | ### 2. Show Product
40 |
41 | `GET`: `{baseUrl}/api/products/:id`
42 |
43 | `Parameters`: No
44 | `Request body`: No
45 | `Response`:
46 | ```
47 | {
48 | "name": "Test product name",
49 | "price": "1000000",
50 | "description": "Test product description",
51 | "id": "rovKMQRGxpAUUQet_dE7W"
52 | }
53 | ```
54 |
55 | ### 3. Create Product
56 |
57 | `POST`: `{baseUrl}/api/products`
58 |
59 | `Parameters`: No
60 | `Request body`:
61 | ```
62 | {
63 | "name": "Iphone 12",
64 | "price": "25000000",
65 | "description": "Thông tin chi tiết sản phẩm Iphone 12"
66 | }
67 | ```
68 | `Response`:
69 | ```
70 | {
71 | "success": true
72 | }
73 | ```
74 |
75 | ### 4. Update Product
76 |
77 | `PUT`: `{baseUrl}/api/products/:id`
78 |
79 | `Parameters`: No
80 | `Request body`:
81 | ```
82 | {
83 | "name": "Iphone 12",
84 | "price": "25000000",
85 | "description": "Thông tin chi tiết sản phẩm Iphone 12"
86 | }
87 | ```
88 | `Response`:
89 | ```
90 | {
91 | "success": true
92 | }
93 | ```
94 |
95 | ### 5. Delete Product
96 |
97 | `PUT`: `{baseUrl}/api/products/:id`
98 |
99 | `Parameters`: No
100 | `Request body`:
101 | ```
102 | {
103 | "name": "Iphone 12",
104 | "price": "25000000",
105 | "description": "Thông tin chi tiết sản phẩm Iphone 12"
106 | }
107 | ```
108 | `Response`:
109 | ```
110 | {
111 | "success": true
112 | }
113 | ```
114 |
115 | ### 6. List User
116 |
117 | `GET`: `{baseUrl}/api/users`
118 |
119 | `Parameters`: No
120 | `Request body`: No
121 | `Response`:
122 | ```
123 | [
124 | .....
125 | {
126 | "id": "5YsFNN42B14VYHhpDyizn",
127 | "name": "Administrator",
128 | "email": "admin@gmail.com",
129 | "password": *****
130 | }
131 | .....
132 | ]
133 | ```
134 |
135 | ### 7. Show User
136 |
137 | `GET`: `{baseUrl}/api/users/:id`
138 |
139 | `Parameters`: No
140 | `Request body`: No
141 | `Response`:
142 | ```
143 | {
144 | "id": "5YsFNN42B14VYHhpDyizn",
145 | "name": "Administrator",
146 | "email": "admin@gmail.com",
147 | "password": ******
148 | }
149 | ```
150 |
151 | ### 8. Create User
152 |
153 | `POST`: `{baseUrl}/api/users`
154 |
155 | `Parameters`: No
156 | `Request body`:
157 | ```
158 | {
159 | "name": "Administrator",
160 | "email": "admin@gmail.com",
161 | "password": 123456
162 | }
163 | ```
164 |
165 | `Response`:
166 | ```
167 | {
168 | "success": true
169 | }
170 | ```
171 |
172 | ### 9. Update User
173 |
174 | `PUT`: `{baseUrl}/api/users/:id`
175 |
176 | `Parameters`: No
177 | `Request body`:
178 | ```
179 | {
180 | "name": "Test user update",
181 | "email": "userupdate@gmail.com",
182 | "password": 123456
183 | }
184 | ```
185 |
186 | `Response`:
187 | ```
188 | {
189 | "success": true
190 | }
191 | ```
192 |
193 | ### 10. Delete User
194 |
195 | `DELETE`: `{baseUrl}/api/users/:id`
196 |
197 | `Parameters`: No
198 | `Request body`: No
199 |
200 | `Response`:
201 | ```
202 | {
203 | "success": true
204 | }
205 | ```
206 |
207 | ### 11. Login
208 |
209 | `POST`: `{baseUrl}/api/login`
210 |
211 | `Parameters`: No
212 | `Request body`:
213 | ```
214 | {
215 | "email": "userupdate@gmail.com",
216 | "password": 123456
217 | }
218 | ```
219 |
220 | `Response`:
221 | ```
222 | {
223 | "user": {
224 | "id": "id",
225 | "name": "Test user update",
226 | "email": "userupdate@gmail.com",
227 | },
228 | token: 'eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI....'
229 | }
230 | ```
231 |
--------------------------------------------------------------------------------