-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
218 lines (160 loc) · 5.83 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const fetch = require("node-fetch");
const mongoose = require('mongoose');
const redis = require("redis");
const client = redis.createClient();
const REDIS_BASIC = require('./model/redis_basic');
const PHOTO_STORE = require('./model/photo-store');
const PHOTO_STORE_COLL = require('./database/photo_store-coll');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static('./public'));
app.set('view engine', 'ejs');
app.set('views', './views');
const listProduct = [
{
_id: 111,
name: "Áo sơ mi trắng",
price: "16$",
image1: "http://bestjquery.com/tutorial/product-grid/demo9/images/img-1.jpg",
image2: "http://bestjquery.com/tutorial/product-grid/demo9/images/img-2.jpg"
},
{
_id: 222,
name: "Áo thun xanh lá",
price: "5$",
image1: "http://bestjquery.com/tutorial/product-grid/demo9/images/img-3.jpg",
image2: "http://bestjquery.com/tutorial/product-grid/demo9/images/img-4.jpg"
},
{
_id: 333,
name: "Áo sáng tạo",
price: "5$",
image1: "http://bestjquery.com/tutorial/product-grid/demo9/images/img-5.jpg",
image2: "http://bestjquery.com/tutorial/product-grid/demo9/images/img-6.jpg"
},
{
_id: 444,
name: "Áo sọc sọc",
price: "6$",
image1: "http://bestjquery.com/tutorial/product-grid/demo9/images/img-7.jpg",
image2: "http://bestjquery.com/tutorial/product-grid/demo9/images/img-8.jpg"
},
]
//========> MENU-CART
app.get('/', async (req, res) => {
res.render("home")
});
app.get('/menu-cart', async (req, res) => {
let nameCart = "myCart";
let myCart = await REDIS_BASIC.myCart({ nameCart });
res.render('menu', {listProduct, myCart });
});
app.get('/add-to-cart/:productID', async (req, res) => {
let { productID } = req.params;
let nameCart = "myCart";
let addtoCart = await REDIS_BASIC.addToCart({ nameCart, productID });
res.json(addtoCart)
})
app.get('/remove-in-cart/:productID', async (req, res) => {
let { productID } = req.params;
let nameCart = "myCart";
let removeInCart = await REDIS_BASIC.removeInCart({ nameCart, productID });
res.json(removeInCart)
})
app.get('/remove-all-cart/:nameCart', async (req, res) => {
let { nameCart } = req.params;
let removeAllCart = await REDIS_BASIC.removeAllCart({ nameCart });
res.json(removeAllCart)
})
//===============> END
app.get('/photos', (req, res) => {
// key to store results in Redis store
const photosRedisKey = 'user:photos';
// Try fetching the result from Redis first in case we have it cached
return client.get(photosRedisKey, (err, photos) => {
// If that key exists in Redis store
if (photos) {
return res.json({ source: 'cache', data: JSON.parse(photos) })
} else { // Key does not exist in Redis store
// Fetch directly from remote api
fetch('https://jsonplaceholder.typicode.com/photos')
.then(response => response.json())
.then(photos => {
// Save the API response in Redis store, data expire time in 3600 seconds, it means one hour
client.setex(photosRedisKey, 3600, JSON.stringify(photos))
// Send JSON response to client
return res.json({ source: 'api', data: photos })
})
.catch(error => {
console.log(error)
// send error to the client
return res.json(error.toString())
})
}
});
});
app.get('/hashes', (req, res) => {
res.render('hashes');
})
//============> THÊM, XÓA HASHES REDIS
app.post('/add-hashes', async (req, res) => {
try {
let { id, key, value } = req.body;
let infoHashes = await REDIS_BASIC.insert({ id, key, value })
res.json(infoHashes.data)
} catch (error) {
console.log(error);
}
})
app.get('/info-hashes/:id', async (req, res) => {
let { id } = req.params;
let infoHashes = await REDIS_BASIC.infoHash({id});
res.json(infoHashes.data)
})
//===========> END
//==========> LẤY VỊ TRÍ VÀ CỬA HÀNG XUNG QUANH
app.post('/add-photo', async (req, res) => {
try {
let { name, place, available } = req.body;
let infoStorePhoto = await PHOTO_STORE.insert({ name, place, available })
res.json(infoStorePhoto)
} catch (error) {
console.log(error);
}
})
app.post('/stores', async (req, res) => {
try {
// let lng = "106.797263"
// let lat = "10.849314"
//let placeHome = "lng, lat";
let { lng, lat, maxDis } = req.body;
let listPhotoStore = await PHOTO_STORE.getList({ lng, lat, maxDis });
res.json(listPhotoStore);
} catch (error) {
console.log(error);
}
});
app.get('/store-near', async (req, res) => {
try {
let { lng, lat } = req.query;
let listPhotoStoreNear = await PHOTO_STORE.getListStoreNear({ lng, lat });
res.json(listPhotoStoreNear);
} catch (error) {
console.log(error);
}
});
app.get('/find-store', async (req, res) => {
res.render("find-store");
});
//==============> END
const uri = 'mongodb://localhost/demo-geo';
const PORT = process.env.PORT || 3000;
mongoose.set('useCreateIndex', true); //ẩn cảnh báo
mongoose.set('useUnifiedTopology', true); // ẩn cảnh báo
mongoose.connect(uri, { useNewUrlParser: true });
mongoose.connection.once('open', () => {
app.listen(PORT, () => console.log(`Server started at PORT ${PORT}`));
});