-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
127 lines (110 loc) · 3.1 KB
/
app.js
File metadata and controls
127 lines (110 loc) · 3.1 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import express from 'express';
import mongoose from 'mongoose';
import Stud_detail from './models/details.js';
import dotenv from "dotenv";
dotenv.config();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
console.log(dotenv.parse);
//const connectDB = async () => {
// try {
//await
mongoose.connect(process.env.DB_URI, {
Name: process.env.DB_NAME,
user: process.env.DB_USER,
pass: process.env.DB_PASS
})
.then((result)=>{
console.log("DB connected successfuly")
})
.catch((error)=>{
console.error(error)
})
//connectDB();
//API to insert data into the Stud_detail DB
app.post('/details', async (req, res) => {
try {
const detail = await new Stud_detail(req.body);
await detail.save();
res.send(detail);
} catch (error) {
console.error(error);
res.status(500).send(error);
}
})
//API to fetch data from DB based on its id
app.get('/details/:id', async (req, res) => {
try {
const id = req.params.id;
// console.log(id);
if (!id) {
//400<500
//Bad request
res.status(400).send("Please send the id");
}
const detail = await Stud_detail.findById(id)
res.send(detail);
} catch (error) {
console.error(error);
res.status(500).send(error)
}
})
//API to fetch whole data from DB
app.get('/details', async (req, res) => {
try {
const detail = await Stud_detail.find({});
res.send(detail);
//console.log(detail);
} catch (error) {
console.error(error);
res.status(500).send(error);
}
})
//API to update data from database based on id
app.put('/details/:id', async (req, res) => {
try {
const id = req.params.id;
// console.log(id);
if (!id) {
//400<500
//Bad request
res.status(400).send("Please send the id");
}
const detail = await Stud_detail.findByIdAndUpdate({ _id:id },
{
$set: {
Stud_name: req.body.Stud_name,
StudClass: req.body.StudClass,
Age: req.body.Age,
adharNo: req.body.AadharNo,
Email: req.body.Email
}
})
res.send(detail)
} catch (error) {
console.error(error);
res.status(500).send(error);
}
})
//API to delete data from DB based on id
app.delete('/details/:id', async (req, res) => {
try {
const id = req.params.id;
if (!id) {
//400<500
//Bad request
res.status(400).send("Please send the id");
}
const detail = await Stud_detail.findByIdAndDelete(id);
res.send(detail);
} catch (error) {
console.log(error);
res.status(500).send(error);
}
})
const port = process.env.PORT || 3000
const host = process.env.DB_HOST
app.listen(port, host, () => {
console.log(`starting server at ${host}: ${port}`);
});