-
-
Save navanathjadhav/bad045ed13cc52e5eadcc948ace7dcad to your computer and use it in GitHub Desktop.
2. Added mock GET students API
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 | |
*/ | |
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", | |
}, | |
]; | |
/* | |
* Get students with query params: [pageNumber, recordsPerPage] | |
*/ | |
app.get("/api/students", (req, res) => { | |
// 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); | |
// Send response: { count, data } | |
res.status(200).json({ | |
count: students.length, | |
data: students.slice(start, end), | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment