-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
134 lines (110 loc) · 3.2 KB
/
server.js
File metadata and controls
134 lines (110 loc) · 3.2 KB
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
const express = require("express");
const app = express()
const UserDB = require('./usersScheema.js');
const mongoose = require("mongoose")
const MongoClient = require('mongodb').MongoClient;
const passsport ="9Qj3xwzGOJxIjSLP";
const MONGODB_URI = "mongodb+srv://solomon:9Qj3xwzGOJxIjSLP@cluster0.zq7nsu8.mongodb.net/test"
const users =[
{name:"solomon", age:"1"},
{name:"somoe", age:"2"},
{name:"solomon", age:"3"},
{name:"kiki", age:"4"},
{name:"solomon", age:"5"},
{name:"solomon", age:"6"},
{name:"solomon", age:"7"},
{name:"solomon", age:"8"},
{name:"solomon", age:"9"},
{name:"solomon", age:"10"},
{name:"solomon", age:"11"},
{name:"solomon", age:"12"},
{name:"solomon", age:"13"},
{name:"solomon", age:"14"},
{name:"solomon", age:"15"},
{name:"solomon", age:"16"},
{name:"solomon", age:"17"},
{name:"moan", age:"18"},
{name:"solomon", age:"19"},
{name:"lesto", age:"20"},
]
app.get("/users", (req, res)=>{
res.json(users)
})
app.get("/paginated", (req, res)=>{
const page = parseInt(req.query.page);
const limit = parseInt(req.query.limit);
console.log(page);
console.log(limit);
const startIndex = (page - 1) * limit;
const endIndex = page * limit;
const results ={};
if(endIndex < users.length){
results.next ={
page:page + 1,
limit:limit
}
}
if (startIndex > 0){
results.previous ={
page:page - 1,
limit:limit
}
}
results.results = users.slice(startIndex, endIndex)
res.json(results)
})
mongoose.connect(MONGODB_URI)
.then(() => {
console.log('Connected to MongoDB!');
})
.catch((error) => {
console.error('Error connecting to MongoDB:', error);
});
app.get('/add/users', async (req, res) => {
try {
console.log("routes hits")
const name = "makena"
const age = 34
const user = new UserDB({
name:name,
age:age,
});
const result = await user.save();
res.status(201).send(result);
} catch (error) {
console.log(error)
res.status(400).send(error);
}
});
app.get("/get/users", async(req, res)=>{
const page = parseInt(req.query.page);
const pageSize = parseInt(req.query.limit);
try{
const totalUsers = await UserDB.countDocuments();
const totalPages = Math.ceil(totalUsers / pageSize);
const paginatedUsers = await UserDB.find()
.skip((page - 1) * pageSize)
.limit(pageSize);
res.status(200).json({
totalUsers,
paginatedUsers,
totalPages,
currentPage: page
});
}catch(error){
console.log(error)
console.error(error);
res.status(500).send('Server Error');
}
})
app.get("/users/getall", async(req, res)=>{
const totalUsers = await UserDB.countDocuments();
const paginatedUsers = await UserDB.find()
res.status(200).json({
totalUsers,
paginatedUsers
});
})
app.listen(5000, ()=>{
console.log("app connected on port 5000")
})