-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
117 lines (88 loc) · 2.77 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
106
107
108
109
110
111
112
113
114
115
116
117
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
// configure app
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
var mongoose = require('mongoose');
mongoose.connect('mongodb://tg:technologygrows@kahana.mongohq.com:10020/tg'); // connect to our database
var Bird = require('./app/models/bird');
// ROUTES FOR OUR API
// =============================================================================
// create our router
var router = express.Router();
// middleware to use for all requests
router.use(function(req, res, next) {
// do logging
console.log('Something is happening.');
next();
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'Technology Grows - Welcome to our api!' });
});
// on routes that end in /birds
// ----------------------------------------------------
router.route('/birds')
// create a bird (accessed at POST http://localhost:8080/birds)
.post(function(req, res) {
var bird = new Bird(); // create a new instance of the Bird model
bird.name = req.body.name; // set the birds name (comes from the request)
bird.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Bird created!' });
});
})
// get all the birds (accessed at GET http://localhost:8080/api/birds)
.get(function(req, res) {
Bird.find(function(err, birds) {
if (err)
res.send(err);
res.json(birds);
});
});
// on routes that end in /birds/:bird_id
// ----------------------------------------------------
router.route('/birds/:bird_id')
// get the bird with that id
.get(function(req, res) {
Bird.findById(req.params.bird_id, function(err, bird) {
if (err)
res.send(err);
res.json(bird);
});
})
// update the bird with this id
.put(function(req, res) {
Bird.findById(req.params.bird_id, function(err, bird) {
if (err)
res.send(err);
bird.name = req.body.name;
bird.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Bird updated!' });
});
});
})
// delete the bird with this id
.delete(function(req, res) {
Bird.remove({
_id: req.params.bird_id
}, function(err, bird) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
// REGISTER OUR ROUTES -------------------------------
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);