-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
128 lines (102 loc) · 3.93 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
118
119
120
121
122
123
124
125
126
127
128
// BASE SETUP
// =============================================================================
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/staffList');
mongoose.connection.on("error", function(err) {
console.log("Could not connect to mongo server!");
return console.log(err);
});
var staff = require('./app/models/staff');
// call the packages we need
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({exnted: true}));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router();
// get an instance of the express Router
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(request, response, next){
console.log('In the middle of something...');
next();
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(request, response){
response.json({message: "Yhoo! I make the API now!!!"});
});
// more routes for our API will happen here
// on routes that end in /bears
// ----------------------------------------------------
router.route('/staff')
.post(function(request, response){
//CORS setup
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "X-Requested-With");
var staffMember = new staff();
staffMember.name = request.body.name;
staffMember.age = request.body.age;
staffMember.address = request.body.address;
console.log(staffMember);
staffMember.save(function(error){
if (error) throw error;
response.json(staffMember);
});
})
.get(function(request, response){
//CORS setup
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "X-Requested-With");
staff.find(function(error, staff){
if (error) throw error;
response.json(staff);
});
})
// on routes that end in /bears/:bear_id
// ----------------------------------------------------
router.route('/staff/:staffID')
.get(function(request, response, corsActivate){
//CORS setup
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "X-Requested-With");
staff.findById(request.params.staffID, function(error, staff){
if (error) throw error;
response.json(staff);
});
})
.put(function(request, response){
//CORS setup
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "X-Requested-With");
staff.findById(request.params.staffID, function(error, staff){
if (error) throw error;
var oldName = staff.name;
staff.name = request.body.name;
staff.age = request.body.age;
staff.save(function(error){
if (error) throw error;
//response.json({message: 'Now '+ oldName + ' is renamed to ' + staff.name + '(aged ' + staff.age + ')'});
});
});
})
.delete(function(request, response){
//CORS setup
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "X-Requested-With");
staff.remove({_id: request.params.staffID}, function(error, staff){
var oldName = request.params.staffID;
if (error) throw error;
//response.json({message: oldName + ' is no longer our staff!'});
});
})
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magician is opening port ' + port);