-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
105 lines (95 loc) · 2.28 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
93
94
95
96
97
98
99
100
101
102
103
104
105
// initialize express
var express = require('express');
var app = express();
// update all requirements
var bodyParser = require('body-parser');
// initialize ORM
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/db');
// allow cows to graze in teh file pasture
var Cow = require('./models/cow');
// configure bodyParser to get data from POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// set port
var port = 1337;
// create router
var router = express.Router();
// register routes to /api
app.use('/api', router);
// middleware to log all reqs
router.use(function(req, res, next) {
// logloglog
console.log('Mooooo!');
// continue to next routes
next();
});
// test router & route for root
router.get('/', function(req, res) {
res.json({ message: 'Welcome to the pasture!' });
});
// routes for /cows
router.route('/cows')
// add a cow
.post(function(req, res) {
var cow = new Cow();
cow.name = req.body.name;
cow.save(function(err) {
if (err) {
res.send(err);
}
res.json({ message: 'Another moo for you!' });
});
})
// get a cow
.get(function(req, res) {
Cow.find(function(err, cows) {
if (err) {
res.send(err);
}
res.json(cows);
})
});
// routes for a specific cow
router.route('/cows/:cow_id')
// get specific cow
.get(function(req, res) {
Cow.findById(req.params.cow_id, function(err, cow) {
if (err) {
res.send(err);
}
res.json(cow);
});
})
// update specific cow
.put(function(req, res) {
// find specific cow
Cow.findById(req.params.cow_id, function(err, cow) {
if (err) {
res.send(err);
}
// update cow info
cow.name = req.body.name;
// save teh cow
cow.save(function(err) {
if (err) {
res.send(err);
}
res.json({ message: 'Oh youz a fancy cow now!'});
});
});
})
// delete specific cow
.delete(function(req, res) {
Cow.remove({
_id: req.params.cow_id
}, function(err, cow) {
if (err) {
res.send(err);
}
res.json({ message: "The Cow has been slaughtered... R.I.P Cow :'("});
});
});
// start server
app.listen(port);
console.log("You're now grazing on port " + port);