-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
107 lines (83 loc) · 2.64 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
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var uuid = require('node-uuid');
// configure app
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
var mongoose = require('mongoose');
mongoose.connect('mongodb://jedd:[email protected]:10094/jedd_staging'); // connect to our database
var Item = require('./app/models/item');
// ROUTES FOR OUR API
// =============================================================================
var apiRouter = express.Router();
apiRouter.use(function(req, res, next) {
console.log('Something is happening.');
next();
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
apiRouter.get('/', function(req, res) {
res.status(200).json({ status: 'OK' });
});
apiRouter.route('/items')
.post(function(req, res) {
var item = new Item();
item._id = uuid.v4();
item.finished = req.body.finished || false;
item.archived = req.body.archived || false;
item.created = new Date();
item.updated = item.created;
item.entry = req.body.entry || "";
item.save(function(err) {
if (err)
res.status(500).send(err);
res.status(200).json({ data: item }); // TODO: Maybe better to get the data from db.
});
})
.get(function(req, res) {
Item.find(req.query.archived ? null : { archived: false }, function(err, items) {
if (err)
res.send(err);
res.json({ data: items });
});
});
apiRouter.route('/items/:item_id')
// update
.put(function(req, res) {
Item.findById(req.params.item_id, function(err, item) {
if (err)
res.status(500).send(err);
item.finished = req.body.finished || false;
item.updated = new Date();
item.entry = req.body.entry || item.entry;
item.save(function(err) {
if (err)
res.status(500).send(err);
res.json({ data: item });
});
});
})
// archive.
.delete(function(req, res) {
Item.findById(req.params.item_id, function(err, item) {
if (err)
res.status(500).send(err);
item.archived = true;
item.save(function(err) {
if (err)
res.status(500).send(err);
res.json({ data: item._id });
})
});
});
// REGISTER OUR ROUTES -------------------------------
app.use('/api', apiRouter);
app.use(express.static(__dirname + '/client'));
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);