-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
159 lines (127 loc) · 3.37 KB
/
app.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require('dotenv').config()
const express = require('express')
const app = express()
const port = process.env.PORT || 5000;
const { MongoClient, ServerApiVersion } = require('mongodb');
const ObjectId = require('mongodb').ObjectId;
const bodyParser = require('body-parser')
// set the view engine to ejs
let path = require('path');
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(bodyParser.urlencoded({ extended: true }))
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(process.env.MONGO_URI, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
async function getInfoData() {
try {
// Connect the client to the server (optional starting in v4.7)
await client.connect();
// Send a ping to confirm a successful connection
// await client.db("admin").command({ ping: 1 });
const result = await client
.db("quebec-database")
.collection("quebec-collection")
.find()
.toArray();
console.log("mongo call await inside function: ", result);
return result;
//await client.db("admin").command({ ping: 1 });
//console.log("Pinged your deployment. You successfully connected to MongoDB!");
}
catch (err) {
console.log("getInfoData() error:", err);
}
finally {
// Ensures that the client will close when you finish/error
// await client.close();
}
}
// read from mongo
app.get('/', async (req, res) => {
let result = await getInfoData()
.catch(console.error);
console.log("getInfoData() result:", result);
res.render('index', {
pageTitle: "Information",
infoData: result
});
});
// create to mongo
app.post('/addInfo', async (req, res) => {
try {
client.connect;
const collection = client
.db("quebec-database")
.collection("quebec-collection");
//draws from body parser
console.log(req.body);
await collection.insertOne(req.body);
res.redirect('/');
}
catch (err) {
console.log(err)
}
finally {
}
});
app.post('/updateInfo', async (req, res) => {
try {
console.log("req.body: ", req.body)
client.connect;
const collection = client
.db("quebec-database")
.collection("quebec-collection");
let result = await collection.findOneAndUpdate({
"_id": new ObjectId(req.body.id)
},
{
$set:
{
name: req.body.name,
country: req.body.country,
job: req.body.job,
hobby: req.body.hobby,
note: req.body.note
}
}
)
.then(result => {
console.log(result);
res.redirect('/');
})
.catch(error => console.error(error));
res.status(500).send("Internal Server Error");
}
finally {
}
});
app.post('/deleteInfo', async (req, res) => {
try {
client.connect;
const collection = client
.db("quebec-database")
.collection("quebec-collection");
//draws from body parser
console.log(req.body);
let result = await collection.findOneAndDelete(
{
"_id": new ObjectId(req.body.id)
}
)
res.redirect('/');
}
catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
} finally {
}
});
app.listen(port, () => {
console.log(`Quebec app listening on port ${port}`)
})