-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
131 lines (109 loc) · 3.66 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
const express = require('express');
const bodyParser = require('body-parser');
const app = express(); // express instance
const PORT = 3000;
app.use(bodyParser.json());
const path = require('path'); // what is path package used for?
const db = require('./db'); // this?
const collection = 'trackers';
// configuring app routes
app.use(express.static('./dist')); //static route (current parcel workaround)
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, './dist/index.html'));
});
// get documents from databast (GET)
app.get('/getTodos', (req, res) => {
db.getDB()
.collection(collection)
.find({})
.toArray((err, documents) => {
if (err) console.log(err);
else {
console.log(documents);
res.json(documents);
}
});
});
// updating an existing document in the database (UPDATE)
app.put('/:id', (req, res) => {
const trackerID = req.params.id;
const userInput = req.body;
db.getDB()
.collection(collection)
.findOneAndUpdate({ _id: db.getPrimaryKey(trackerID) }, { $set: { tracker: userInput.tracker } }, { returnOriginal: false }, (err, result) => {
if (err) console.log('update failed with error: ' + err);
else res.json(result);
});
});
// create a new document in the database (POST)
app.post('/', (req, res) => {
const userInput = req.body;
db.getDB()
.collection(collection)
.insertOne(userInput, (err, result) => {
if (err) console.log('unable to create document: ' + err);
else res.json({ result: result, document: result.ops[0] });
});
});
// delete a existing document from the db (DELETE)
app.delete('/:id', (req, res) => {
const trackerID = req.params.id;
db.getDB()
.collection(collection)
.findOneAndDelete({ _id: db.getPrimaryKey(trackerID) }, (err, result) => {
if (err)
console.log("could not delete: "+ err);
else
res.json(result);
});
});
// connect to database
db.connect((err) => {
if (err) {
console.log('Unable to connect to database');
process.exit(1);
} else {
app.listen(PORT, () => {
console.log(`Connect to database, app listening on port ${PORT}`);
});
}
});
// code example from mongoDB
// const MongoClient = require('mongodb').MongoClient;
// const uri = "mongodb+srv://foolancer:[email protected]/foolance?retryWrites=true&w=majority";
// const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
// client.connect(err => {
// const collection = client.db("foolance").collection("trackers");
// // perform actions on the collection object
// console.log(collection);
// client.close();
// });
// solution from https://www.youtube.com/watch?v=M9Fs-CCe0Jo
// is throwing the same dns error...
// connect();
// async function connect() {
// const client = new MongoClient(uri);
// try {
// await client.connect();
// const db = client.db(`foolance`);
// console.log(`Connected to database ${db.databaseName}`);
// }
// catch (ex) {
// console.error(`Exception: ${ex}`);
// }
// finally {
// client.close();
// }
// }
// sevis solution
// async function main() {
// const url = 'mongodb://localhost:27017';
// const client = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true });
// await client.connect();
// const collection = client.db("foolance").collection("trackers");
// await collection.insertOne({ name: 'jan', hours: 3 });
// await collection.insertOne({ name: 'sev', hours: 1 });
// var docs = await collection.find({}).toArray();
// console.log(docs);
// await client.close();
// };