routes.js
1.69 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
// app/routes.js
// grab the nerd model we just created
var Nerd = require('./models/nerd');
module.exports = function (app) {
// server routes ===========================================================
// handle things like api calls
// authentication routes
// sample api route
app.get('/api/nerds', function (req, res) {
// use mongoose to get all nerds in the database
Nerd.find(function (err, nerds) {
if (err)
res.send(err);
res.json(nerds);
});
});
app.post('/api/nerds', function (req, res) {
Nerd.create({
login: req.body.login,
role: req.body.role,
section: req.body.section,
}, function (err, nerd) {
if (err)
res.send(err);
Nerd.find(function (err, nerds) {
if (err)
res.send(err);
res.json(nerds);
});
});
});
app.delete('/api/nerds/:nerd_id', function (req, res) {
Nerd.remove({
_id: req.params.nerd_id
}, function (err, nerd) {
if (err)
res.send(err);
Nerd.find(function (err, nerds) {
if (err)
res.send(err);
res.json(nerds);
});
})
})
// route to handle creating goes here (app.post)
// route to handle delete goes here (app.delete)
// frontend routes =========================================================
// route to handle all angular requests
app.get('*', function (req, res) {
res.sendfile('./public/views/index.html'); // load our public/index.html file
});
};