-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (46 loc) · 1.56 KB
/
server.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
import express from 'express';
import connect from "./database/connection.js";
import seedDatabase from "./database/seed.js";
import TodoItem from "./model/todoitem.model.js";
import lodash from "lodash";
import cors from "cors";
const app = express();
app.use(express.json());
app.use(cors());
const port = process.env.PORT || 8080;
app.post('/todos/add', (req, res) => {
const defaults = {
open: true,
}
console.log("Object prototype before merge: " + JSON.stringify({}.__proto__))
const todoToAdd = lodash.merge(defaults, req.body)
console.log("Object prototype after merge: " + JSON.stringify({}.__proto__))
const todoItem = new TodoItem(todoToAdd);
todoItem.save()
.then(() => res.json({msg: `Successfully added a todo item`}))
.catch(error => res.json({error: error.message}))
})
app.get('/todos/', (req, res) => {
TodoItem.find({visible: true})
.then(data => res.json(data))
.catch(error => res.json({error}))
})
app.get('/todos/:id', (req, res) => {
TodoItem.find({owner: req.params.id})
.then(data => res.json(data))
.catch(error => res.json({error}))
})
connect()
.then(async () => {
await seedDatabase();
try {
app.listen(port, () => {
console.log(`Server connected to http://localhost:${port}`)
})
} catch (error) {
console.log(`Unable to start to the Express server: ${error.message}`)
}
})
.catch((error) => {
console.log(`Invalid database connection: ${error.message}`)
})