forked from scotch-io/node-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
121 lines (91 loc) · 2.85 KB
/
server.js
File metadata and controls
121 lines (91 loc) · 2.85 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
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
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var morgan = require('morgan');
// configure app
app.use(morgan('dev')); // log requests to the console
// configure body parser
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://node:node@novus.modulusmongo.net:27017/Iganiq8o'); // connect to our database
var NPC = require('./app/models/npc');
// 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: 'hooray! welcome to our api!' });
});
// on routes that end in /bears
// ----------------------------------------------------
router.route('/npcs')
// create a bear (accessed at POST http://localhost:8080/bears)
.post(function(req, res) {
var npc = new NPC(); // create a new instance of the Bear model
bear.name = req.body.name; // set the bears name (comes from the request)
npc.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'NPC created!' });
});
})
// get all the bears (accessed at GET http://localhost:8080/api/bears)
.get(function(req, res) {
NPC.find(function(err, npcs) {
if (err)
res.send(err);
res.json(npcs);
});
});
// on routes that end in /bears/:bear_id
// ----------------------------------------------------
router.route('/npcs/:npc_id')
// get the bear with that id
.get(function(req, res) {
NPC.findById(req.params.npc_id, function(err, npc) {
if (err)
res.send(err);
res.json(npc);
});
})
// update the bear with this id
.put(function(req, res) {
NPC.findById(req.params.npc_id, function(err, npc) {
if (err)
res.send(err);
npc.name = req.body.name;
npc.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'NPC updated!' });
});
});
})
// delete the npc with this id
.delete(function(req, res) {
NPC.remove({
_id: req.params.npc_id
}, function(err, npc) {
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);