-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.js
81 lines (75 loc) · 1.54 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const express=require("express");
const todo=require("./todosModel")
const router=express.Router()
// creating a user
router.post('/create', async(req, res) => {
try{
const {name,email,number} = req.body;
console.log(req.body)
const data = await todo.create({name,email,number} );
console.log("Request",req.body)
res.status(201).json({
success:true,
data:data,
});
}
catch(err){
res.status(500).json({
success:false,
msg:"Errror in create "
})
}
});
// get all user data
router.get('/all',async(req,res)=>{
try{
const data=await todo.find({})
res.status(200).json({
success:true,
data:data,
})
}
catch(err){
res.status(500).json({
success:false,
msg:"Errror in getting user"
})
}
})
// find by id and delete
router.put("/delete/:id",async(req,res)=>{
try{
const id=req.params.id;
const user= await todo.findByIdAndDelete(id)
res.status(500).json({
success:true,
data:user,
})
}
catch(err){
res.status(500).json({
success:false,
msg:"Errror in deletion"
})
}
})
//find by id and update
router.put("/update/:id",async(req,res)=>{
try{
const id=req.params.id;
const {name,email,number}=req.body;
const user=await todo.findByIdAndUpdate(id,{name,email,number})
res.status(200).json({
success:true,
data:user,
msg:"User data Updated"
})
}
catch(err){
res.status(500).json({
success:false,
msg:"Errror in update"
})
}
})
module.exports=router