[text] express1.js

Viewer

copydownloadembedprintName: express1.js
  1. const express=require("express");
  2. const { name } = require("nodeman/lib/mustache");
  3. const app=express();
  4. app.set("view engine","ejs");
  5. let contactlist=[
  6.     {
  7.         id:"1",
  8.         name:"chinnu",
  9.         contact:"6300323832"
  10.     },
  11.     {
  12.         id:"2",
  13.         name:"Harshith",
  14.         contact:"9492001177"
  15.     },
  16.     {
  17.         id:"3",
  18.         name:"Khagendar",
  19.         contact:"9381988798"
  20.     },
  21.     {
  22.         id:"4",
  23.         name:"Rishik",
  24.         contact:"9528666677"
  25.     }
  26. ];
  27. app.get("/contact",(req,res)=>{
  28.     res.render("contact",{
  29.         contactlist
  30.     });
  31. })
  32. app.get("/contact/:id",(req,res)=>{
  33.     let flag=0;
  34.     contactlist.forEach((contact)=>{
  35.         if(req.params.id==contact.id)
  36.         {
  37.             flag=1;
  38.             res.render("index",{contact});
  39.         }
  40.         else{
  41.             flag=0;
  42.         }
  43.     })
  44.     if(flag==0)
  45.     {
  46.         res.send("the requested id doesnot match with contactlist");
  47.     }
  48. })
  49. app.listen(3000,()=>{
  50.     console.log("at port 3000 listening");
  51. })

Editor

You can edit this paste and save as new:


File Description
  • express1.js
  • Paste Code
  • 25 Apr-2024
  • 1.03 Kb
You can Share it: