-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainApp.js
72 lines (66 loc) · 1.52 KB
/
MainApp.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
const express = require('express');
const Student=require("./database/models/students")
require('./database/connection');
const app=express();
const port=process.env.PORT || 3000;
app.use(express.json());
//create a new student
app.post("/students",async(req,res)=>{
try{
const user= new Student(req.body)
const result=await user.save()
res.status(201).send(result)
}catch(e){
res.status(400).send(e);
}
})
app.get("/student/:id",async(req,res)=>{
try{
const id=req.params.id;
const result =await Student.findById({_id:id})
if (!result) {
return res.status(500).send();
} else {
res.status(201).send(result)
}
}catch(e){
res.status(404).send(e)
}
})
app.get("/students",async(req,res)=>{
try {
const result=await Student.find()
res.status(201).send(result)
} catch (e) {
res.status(404).send(e)
}
})
//update the students by their id
app.patch("/students/:id",async(req,res)=>{
try {
const _id=req.params.id;
const result= await Student.findByIdAndUpdate(_id,req.body,{
new:true
})
res.send(result)
} catch (e) {
res.status(404).send(e);
}
})
// sare Delete request ko sambalna hai yaha
app.delete("/student/:id",async(req,res)=>{
try {
const _id=req.params.id;
console.log(_id);
const result= await Student.findByIdAndDelete(_id)
if(!result){
return res.status(404)
}
res.send(result)
} catch (e) {
res.status(500).send(e)
}
})
app.listen(port,()=>{
console.log("connected at "+port);
})