Server.js with start & end position calculation by received query parameters: recordsPerPage, pageNumber & searchTerm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment