-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverJS.js
169 lines (141 loc) · 2.83 KB
/
serverJS.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
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const User = require('./users');
mongoose.connect(
'mongodb://0.0.0.0:27017/pagination',
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
const db = mongoose.connection;
db.once('open', async () => {
if(await User.countDocuments().exec() > 0){
return;
}
Promise.all([
User.create({
name: 'User 1',
}),
User.create({
name: 'User 2',
}),
User.create({
name: 'User 3',
}),
User.create({
name: 'User 4',
}),
User.create({
name: 'User 5',
}),
User.create({
name: 'User 6',
}),
User.create({
name: 'User 7',
}),
User.create({
name: 'User 8',
}),
User.create({
name: 'User 9',
}),
User.create({
name: 'User 10',
}),
User.create({
name: 'User 11',
}),
User.create({
name: 'User 12',
}),
User.create({
name: 'User 13',
}),
]).then(() => {
console.log('Added Users');
});
});
app.get('/users', paginatedResults(User), (req, res) => {
res.header('Access-Control-Allow-Origin', '*');
res.sendFile(__dirname + '/index.html');
});
app.listen(3000);
function paginatedResults(model){
return async (req, res, next) => {
const page = parseInt(req.query.page);
const limit = parseInt(req.query.limit);
const results = {};
const startIndex = (page - 1) * limit;
const endIndex = page * limit;
if(endIndex < await model.countDocuments().exec()){
results.next = {
page: page + 1,
limit: limit,
};
}
if(startIndex > 0){
results.previous = {
page: page - 1,
limit: limit,
};
}
try{
results.results = await model
.find()
.limit(limit)
.skip(startIndex)
.exec();
next();
res.paginatedResults = results;
} catch (e){
console.log(res.status(500).json(
{
message: e.message,
}
));
}
};
}
/*
app.get('/users', (req, res) => {
const resultsIterator = paginatedResults(User);
const page = parseInt(req.query.page) || 1;
// Advance the iterator until we reach the desired page
for (let i = 1; i < page; i++) {
resultsIterator.next();
}
const { value, done } = resultsIterator.next();
if (done) {
res.status(404).json({ message: 'Page not found' });
} else {
res.json({
page: value.page,
limit: value.limit,
results: value.results
});
}
});
*/
/*
function* paginatedResults(model) {
const page = parseInt(req.query.page);
const limit = parseInt(req.query.limit);
const startIndex = (page - 1) * limit;
const endIndex = page * limit;
if (endIndex < yield model.countDocuments().exec()) {
yield { page: page + 1, limit: limit };
}
if (startIndex > 0) {
yield { page: page - 1, limit: limit };
}
const results = yield model
.find()
.limit(limit)
.skip(startIndex)
.exec();
return results;
}
*/