-
-
Save navanathjadhav/5634158fb94d47ee06e7135ecd42f54f to your computer and use it in GitHub Desktop.
Server.js with filtered students by search term
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", | |
}, | |
]; | |
/* | |
* Get students | |
*/ | |
app.get("/api/students", (req, res) => { | |
let studentsInternal = students; | |
// Match if searchTerm is received from client | |
// Your DB queries will use searchTerm for matching | |
if (req.query.searchTerm) { | |
studentsInternal = 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(() => { | |
res.status(200).json(studentsInternal); | |
}, 2000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment