-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-no-mongoDB.js
72 lines (60 loc) · 1.62 KB
/
server-no-mongoDB.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
const express = require('express')
const app = express()
const port = 3000
const users = [
{ id: 1, name: 'User 1'},
{ id: 2, name: 'User 2'},
{ id: 3, name: 'User 3'},
{ id: 4, name: 'User 4'},
{ id: 5, name: 'User 5'},
{ id: 6, name: 'User 6'},
{ id: 7, name: 'User 7'},
{ id: 8, name: 'User 8'},
{ id: 9, name: 'User 9'},
{ id: 10, name: 'User 10'},
]
const posts = [
{ id: 1, name: 'Post 1'},
{ id: 2, name: 'Post 2'},
{ id: 3, name: 'Post 3'},
{ id: 4, name: 'Post 4'},
{ id: 5, name: 'Post 5'},
{ id: 6, name: 'Post 6'},
{ id: 7, name: 'Post 7'},
{ id: 8, name: 'Post 8'},
{ id: 9, name: 'Post 9'},
{ id: 10, name: 'Post 10'},
]
// http://localhost:3000/users?page=1&limit=5
app.get('/posts', paginatedResults(posts), (req, res) => {
res.json(res.paginatedResults)
})
app.get('/users', paginatedResults(users),(req, res) => {
res.json(res.paginatedResults)
})
function paginatedResults(model){
return (req, res, next) => {
const page = +req.query.page
const limit = +req.query.limit
const startIndex = (page - 1 ) * limit
const endIndex = (page * limit)
const results = {}
if (endIndex < model.length) {
results.next = {
page: page + 1,
limit: limit
}
}
if (startIndex > 0) {
results.previous = {
page: page - 1,
limit: limit
}
}
results.data = model.slice(startIndex, endIndex)
res.paginatedResults = results
// next will go into the next change of events which will be req, res in the route
next()
}
}
app.listen(port, () => console.log(`Example app listening on port ${port}!`))