Advertisement
shopnilSS

Untitled

Mar 11th, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var getExpress = require("express");
  2. // const { get } = require("node:http");
  3. var ex = getExpress();
  4. var port = 8080;
  5.  
  6. ex.listen(port , ()=>console.log(`Server is running in ${port}`))//creat the server
  7. var student = [
  8.     {
  9.         name: "Rahim",
  10.         id: 1,
  11.         eyeColor:"black",
  12.         age: 18
  13.     },
  14.     {
  15.         name: "Karim",
  16.         id: 2,
  17.         eyeColor:"black",
  18.         age: 19
  19.     },
  20.     {
  21.         name: "Fahim",
  22.         id: 3,
  23.         eyeColor:"black",
  24.         age: 20
  25.     },
  26.     {
  27.         name: "Ridom",
  28.         id: 4,
  29.         eyeColor:"black",
  30.         age: 14
  31.     },
  32. ]//this is my data
  33.  
  34. //Find them whose eye color is black and age greater than 15
  35. ex.get("/student1" , (req , res)=>{
  36.     var eyeColor = req.query.eyeColor;
  37.     var age = req.query.age;
  38.     var getTheStudent = student.filter(val=> val.eyeColor == eyeColor && val.age > age) //filter the data
  39.    
  40.     setTimeout(()=>{
  41.         console.log(getTheStudent);
  42.         res.send(getTheStudent)
  43.     },3000)  //receive the data after 3 sec.
  44.    
  45. })
  46.  
  47.  
  48. //Find them whose age is less than equal 15 and change their eye color brown to black.
  49. ex.get("/student2" , (req , res)=>{
  50.     var age = req.query.age;
  51.     var getTheStudent = student.filter(val=>{
  52.         if(val.age <= age){
  53.             return val.eyeColor = "brown";
  54.         }
  55.     })//filter the data and change the eye color from black to brown
  56.  
  57.     setTimeout(()=>{
  58.         console.log(getTheStudent);
  59.         res.send(getTheStudent)
  60.     },3000)   //receive the data after 3 sec.
  61.  
  62. })
  63.  
  64.  
  65. //Create a get route to see all students name and id only.
  66. ex.get("/student" , (req , res)=>{
  67.     var getOnlyNameAndId = [];
  68.      student.map(val=>{
  69.         var jsonFormat = JSON.stringify(val ,["name","id"]); //receive the id and name in json format
  70.         var convertJsonToObjcet = JSON.parse(jsonFormat);//convert it to object
  71.         getOnlyNameAndId.push(convertJsonToObjcet);//pust the data in to a array
  72.     })
  73.     setTimeout(()=>{
  74.         console.log(getOnlyNameAndId);
  75.         res.send(getOnlyNameAndId)
  76.     },3000)//receive the data after 3 sec.
  77.    
  78. })
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement