Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var getExpress = require("express");
- // const { get } = require("node:http");
- var ex = getExpress();
- var port = 8080;
- ex.listen(port , ()=>console.log(`Server is running in ${port}`))//creat the server
- var student = [
- {
- name: "Rahim",
- id: 1,
- eyeColor:"black",
- age: 18
- },
- {
- name: "Karim",
- id: 2,
- eyeColor:"black",
- age: 19
- },
- {
- name: "Fahim",
- id: 3,
- eyeColor:"black",
- age: 20
- },
- {
- name: "Ridom",
- id: 4,
- eyeColor:"black",
- age: 14
- },
- ]//this is my data
- //Find them whose eye color is black and age greater than 15
- ex.get("/student1" , (req , res)=>{
- var eyeColor = req.query.eyeColor;
- var age = req.query.age;
- var getTheStudent = student.filter(val=> val.eyeColor == eyeColor && val.age > age) //filter the data
- setTimeout(()=>{
- console.log(getTheStudent);
- res.send(getTheStudent)
- },3000) //receive the data after 3 sec.
- })
- //Find them whose age is less than equal 15 and change their eye color brown to black.
- ex.get("/student2" , (req , res)=>{
- var age = req.query.age;
- var getTheStudent = student.filter(val=>{
- if(val.age <= age){
- return val.eyeColor = "brown";
- }
- })//filter the data and change the eye color from black to brown
- setTimeout(()=>{
- console.log(getTheStudent);
- res.send(getTheStudent)
- },3000) //receive the data after 3 sec.
- })
- //Create a get route to see all students name and id only.
- ex.get("/student" , (req , res)=>{
- var getOnlyNameAndId = [];
- student.map(val=>{
- var jsonFormat = JSON.stringify(val ,["name","id"]); //receive the id and name in json format
- var convertJsonToObjcet = JSON.parse(jsonFormat);//convert it to object
- getOnlyNameAndId.push(convertJsonToObjcet);//pust the data in to a array
- })
- setTimeout(()=>{
- console.log(getOnlyNameAndId);
- res.send(getOnlyNameAndId)
- },3000)//receive the data after 3 sec.
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement