-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
94 lines (71 loc) · 3.19 KB
/
app.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
// ----------------------------------------------------------------------------
// ---- CONFIGURATION ---------------------------------------------------------
// ----------------------------------------------------------------------------
var express = require('express'),
mongo = require('mongodb'),
mongoose = require('mongoose'),
bodyParser = require('body-parser');
var mongoUri = 'mongodb://heroku_wx2l3rwk:[email protected]:19033/heroku_wx2l3rwk';
mongoose.connect(mongoUri);
// Define models of the database
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var Channel = new Schema({
title : String,
history : [{activated : Date}],
count : Number,
created : {username : String, timestamp : Date}
});
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
// ----------------------------------------------------------------------------
// ---- AUTHENTICATION --------------------------------------------------------
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// ---- ROUTING ---------------------------------------------------------------
// ----------------------------------------------------------------------------
app.get('/', function(request, response) {
response.send('Hello world!');
});
// Create a new channel on the database
app.put('/channel', function(req, res) {
// Make sure that each required field is present
// i.e. title
if(!req.body.title) {
res.status(400).send({success: false, err: "A title must be indicated"});
return;
}
// Make sure that this title was not used previously
var channelModel = mongoose.model('Channel', Channel);
channelModel.find({title: req.body.title}, function (err, docs) {
if(docs.length > 0) {
res.status(400).send({success: false, err: "That channel already exists!"});
return;
} else {
// If not, add the channel! TODO: Add user and time of creation
var instance = new channelModel();
instance.title = req.body.title;
instance.save(function (err) {
res.send({id: instance._id, success : !err, err: err});
});
}
});
});
// Get a channel by the id that was given upon creation
app.get('/channel/:id', function(req, res) {
var channelModel = mongoose.model('Channel', Channel);
channelModel.findOne({_id: req.params.id}, function(err, channel) {
res.send({channel: channel, err: err});
});
});
// ----------------------------------------------------------------------------
// ---- START APP -------------------------------------------------------------
// ----------------------------------------------------------------------------
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});