[text] We internal

Viewer

copydownloadembedprintName: We internal
  1. const express=require("express")
  2. const app=express()
  3. const bodyparser=require("body-parser")
  4. app.use(bodyparser.json())
  5. const employees=[
  6.     {id:1,empname:"Sachin",dept:"CSE",address:"Hyd"},
  7.     {id:2,empname:"Karthik",dept:"CSE",address:"Warangal"},
  8.     {id:3,empname:"sandeep",dept:"ECE",address:"Vizag"},
  9. ];
  10.  
  11. app.get("/api/employees",(req,res)=>{
  12.     res.json(employees)
  13. })
  14.  
  15. app.get("/api/employees/:id",(req,res)=>{
  16.     var flag=false
  17.     employees.forEach(employee => {
  18.         if(employee.id == req.params.id){
  19.             flag=true
  20.             res.json(employee);
  21.         }
  22.     })
  23.     if(! flag)
  24.         res.json({message:the `employee with id:${req.params.id} is not found`})
  25. })
  26.  
  27. app.post("/api/employees",(req,res)=>{
  28.     const employee={
  29.         id: employees.length + 1,
  30.         empname: req.body.empname,
  31.         dept: req.body.dept,
  32.         address: req.body.address
  33.     }
  34.     employees.push(employee)
  35.     res.json(employees)
  36. })
  37.  
  38. app.put("/api/employees/:id",(req,res)=>{
  39.     const employee=employees.find(
  40.     (employee)=>employee.id==parseInt(req.params.id)
  41.     );
  42.     if(!employee){
  43.         res.json({message:`employee with ${req.params.id} does not exists`});
  44.     }
  45.     else{
  46.         employee.empname=req.body.empname
  47.         employee.dept=req.body.dept
  48.         employee.address=req.body.address
  49.     }
  50.     res.json(employees);
  51.    });
  52.  
  53. app.delete("/api/employees/:id",(req,res)=>{
  54.     const employee=employees.find(
  55.     (employee)=>employee.id==parseInt(req.params.id)
  56.     );
  57.     if(!employee){
  58.     res.json({message:`employee with ${req.params.id} does not exists`});
  59.     }
  60.     var index=employees.indexOf(employee);
  61.     employees.splice(index,1);
  62.     res.json(employee);
  63. });
  64.  
  65. app.listen(1456);

Editor

You can edit this paste and save as new:


File Description
  • We internal
  • Paste Code
  • 09 May-2024
  • 1.73 Kb
You can Share it: