-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
120 lines (108 loc) · 3.14 KB
/
server.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
const http = require("http");
const express = require("express");
const app = express();
/*
* Define hostname & PORT
*/
const hostname = "localhost";
const port = 4000;
/*
* CORS
*/
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, access_token"
);
res.header("Access-Control-Allow-Methods", "PUT, GET, POST, OPTIONS, DELETE");
next();
});
app.use(
express.urlencoded({
extended: true,
})
);
app.use(express.json());
/*
* Mock students data
*/
const students = [
{ name: "Alice Doe", email: "alice@xyz.com", department: "Computer Science" },
{
name: "John Doe",
email: "john@xyz.com",
department: "Information Technology",
},
{ name: "Alice Doe", email: "alice@xyz.com", department: "Computer Science" },
{
name: "John Doe",
email: "john@xyz.com",
department: "Information Technology",
},
{ name: "Alice Doe", email: "alice@xyz.com", department: "Computer Science" },
{
name: "John Doe",
email: "john@xyz.com",
department: "Information Technology",
},
{ name: "Alice Doe", email: "alice@xyz.com", department: "Computer Science" },
{
name: "John Doe",
email: "john@xyz.com",
department: "Information Technology",
},
{ name: "Alice Doe", email: "alice@xyz.com", department: "Computer Science" },
{
name: "John Doe",
email: "john@xyz.com",
department: "Information Technology",
},
{ name: "Alice Doe", email: "alice@xyz.com", department: "Computer Science" },
{
name: "John Doe",
email: "john@xyz.com",
department: "Information Technology",
},
{ name: "Alice Doe", email: "alice@xyz.com", department: "Computer Science" },
{
name: "John Doe",
email: "john@xyz.com",
department: "Information Technology",
},
{ name: "Alice Doe", email: "alice@xyz.com", department: "Computer Science" },
];
/*
* Get students with query params: [pageNumber, recordsPerPage & searchTerm]
*/
app.get("/api/students", (req, res) => {
let studentInternal = students;
// Calculate start, Aka skip if you use it in db queries
const start =
parseInt(req.query.recordsPerPage) * (parseInt(req.query.pageNumber) - 1);
// Calculate end, Aka limit if you use it in db queries
const end = start + parseInt(req.query.recordsPerPage);
// Match if searchTerm is received from client
// Use your DB query here
if (req.query.searchTerm) {
studentInternal = students.filter((student) => {
return (
student.name.match(new RegExp(req.query.searchTerm, "i")) ||
student.email.match(new RegExp(req.query.searchTerm, "i")) ||
student.department.match(new RegExp(req.query.searchTerm, "i"))
);
});
}
// Artificial delay for showing loader in client
setTimeout(() => {
// Send response: { count, data }
res.status(200).json({
count: studentInternal.length,
data: studentInternal.slice(start, end),
});
}, 1000);
});
const server = http.createServer(app);
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});