-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathapi.js
64 lines (50 loc) · 1.5 KB
/
api.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
const cors = require('cors');
const express = require('express');
const helmet = require('helmet');
const db= require('./db/index.js')
const PORT = 8092;
const app = express();
module.exports = app;
app.use(require('body-parser').json());
app.use(cors());
app.use(helmet());
app.options('*', cors());
app.get('/products/search', async(req, response) => {
try{
let res;
let meta;
if(req.query.brand){
res = await db.findPage(parseInt(req.query.page),parseInt(req.query.size),{'brand': req.query.brand});
meta = await db.getMeta(parseInt(req.query.page),parseInt(req.query.size),{'brand': req.query.brand});
}
else{
res = await db.findPage(parseInt(req.query.page),parseInt(req.query.size));
meta = await db.getMeta(parseInt(req.query.page),parseInt(req.query.size));
}
let products = {
"success" : true,
"data" : {
"result" : res,
"meta": meta
}}
response.send(products);
}catch(e){
response.send(e)
}
})
app.get('', async (req, response) => {
console.log("was requested pagination");
let res = await db.findPage(parseInt(req.query.page),parseInt(req.query.size))
let meta = await db.getMeta(parseInt(req.query.page),parseInt(req.query.size))
let products = {
"success" : true,
"data" : {
"result" : res,
//"meta" : {"currentPage":req.query.page,"pageCount":?,"pageSize":res.length,"count":?}
"meta": meta
}
}
response.send(products);
});
app.listen(PORT);
console.log(`📡 Running on port ${PORT}`);