[text] contactlist

Viewer

copydownloadembedprintName: contactlist
  1. app.js
  2.  
  3. const express=require("express");
  4. const app=express();
  5. app.set("view engine","ejs");
  6.  
  7. var contactsList=[
  8.     {
  9.         id:"1",
  10.         name:"Sachin",
  11.         contact:"1234567891",
  12.     },
  13.     {
  14.         id:"2",
  15.         name:"Tendulkar",
  16.         contact:"2345678912",
  17.     },
  18. ]
  19. app.get("/",(req,res)=>{
  20.     res.send("Hello world,This is using node.....");
  21. });
  22. app.get("/contacts",(req,res)=>{
  23.     res.render("contact",{contactsList});
  24. });
  25. app.get("/contacts/:id",(req,res)=>{
  26.     var found=false;
  27.     contactsList.forEach((contact)=>{
  28.            if(req.params.id==contact.id){
  29.             found=true;
  30.             res.render("index",{contact});
  31.            }
  32.            else{
  33.             found=false;
  34.            }
  35.     });
  36.     if(!found){
  37.         res.send("Requested id doesn't match with contacts");
  38.     }
  39. })
  40. app.listen(3001);
  41.  
  42.  
  43. contact.ejs
  44.  
  45. <!DOCTYPE html>
  46. <html lang="en">
  47. <head>
  48.     <meta charset="UTF-8">
  49.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  50.     <title>Document</title>
  51.     <style>
  52.         body,table{
  53.             background-color: aqua;
  54.         }
  55.     </style>
  56. </head>
  57. <body>
  58.     <table border="1px solid cyan" align="center">
  59.         <tr>
  60.             <th>ID</th>
  61.             <th>NAME</th>
  62.             <th>CONTACT</th>
  63.         </tr>
  64.         <%contactsList.forEach((contact)=>{%>
  65.         <tr>
  66.             <td><%= contact.id%></td>
  67.             <td><%= contact.name%></td>
  68.             <td><%= contact.contact%></td>
  69.         </tr>
  70.         <%});%>
  71.     </table>
  72. </body>
  73. </html>
  74.  
  75. index.ejs
  76.  
  77. <!DOCTYPE html>
  78. <html lang="en">
  79. <head>
  80.     <meta charset="UTF-8">
  81.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  82.     <title>Document</title>
  83.     <style>
  84.         table,body{
  85.             background-color: brown;
  86.         }
  87.     </style>
  88. </head>
  89. <body>
  90.     <table border="1px solid cyan" align="center">
  91.         <tr>
  92.             <th>ID</th>
  93.             <th>NAME</th>
  94.             <th>CONTACT</th>
  95.         </tr>
  96.         <tr>
  97.             <td><%= contact.id%></td>
  98.             <td><%=contact.name%></td>
  99.             <td><%=contact.contact%></td>
  100.         </tr>
  101.     </table>
  102. </body>
  103. </html>

Editor

You can edit this paste and save as new:


File Description
  • contactlist
  • Paste Code
  • 09 May-2024
  • 2.19 Kb
You can Share it: