-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
92 lines (78 loc) · 2.15 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
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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const Set = require('./model/set');
const env = require('./env');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
const port = process.env.port || 8080;
const router = express.Router();
//--- ROUTES -------------------------------------------------------------------
router.use((req, res, next) => {
console.log('It is known that now... ');
next();
})
router.get('/', (req, res) => {
res.json({message: 'You know nothing, Jon Snow!'})
})
router.route('/sets')
.post((req, res) => {
const set = new Set();
set.title = req.body.title;
set.save((err) => {
if(err) {
res.send(err);
}
res.json({message: 'set created!'});
})
})
.get((req, res) => {
Set.find((err, sets) => {
if(err) {
res.send(err);
}
res.json(sets)
})
});
router.route('/sets/:set_id')
.get((req, res) => {
Set.findById(req.params.set_id, (err, set) => {
if (err) {
res.send(err);
}
res.json(set);
})
})
.put((req, res) => {
Set.findById(req.params.set_id, (err, set) => {
if (err) {
res.send(err);
}
set.title = req.body.title;
set.save((err) => {
if (err) {
err.send();
}
res.json({message: 'set updated!'});
})
})
})
.delete((req, res) => {
Set.remove({
_id: req.params.set_id
},
(err, set) => {
if (err) {
err.send();
}
res.json({message: 'Successfuly deleted!'});
})
});
app.use('/api', router);
app.listen(port);
console.log('Winter is coming on port ' + port);
//--- DB ----------------------------------------------------------------------
const mongoose = require('mongoose');
mongoose.connect(`mongodb://${env.db.user}:${env.db.pass}@ds113713.mlab.com:13713/memabulary`, {
useMongoClient: true,
});