-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
66 lines (56 loc) · 1.7 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
var path = require('path');
var express = require('express');
var app = express();
var bodyparser = require('body-parser');
var r = require('rethinkdb');
var _ = require('lodash');
var cons = require('consolidate');
var Handlebars = require('handlebars');
var compression = require('compression');
// Set up Handlebars for view templating, using path to ensure Express can find the views regardless of where the app gets launched from
app.set('views', path.resolve(__dirname + '/src'));
app.set('view engine', 'hbs');
app.engine('hbs', cons.handlebars);
// DB configuration
var dbConfig = {
db: 'wedding',
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 28015
};
// GZIP
app.use(compression());
// Short circuit static assets
app.use(express.static(__dirname + '/public'));
app.use(bodyparser.urlencoded({extended: true}));
app.use(bodyparser.json());
app.get('/', function(req, res){
if(!req.query.admin) {
res.status(200).render('index', {bride: 'Katie', groom: 'Joe'});
} else {
res.status(200).render('index', {admin: true});
}
});
app.post('/bus', function(req, res){
var passenger = {
created: r.now(),
name: req.body.passenger
};
r.connect(dbConfig)
.then(function(conn){
r.table('bus').insert(passenger).run(conn)
.then(function(response){
res.status(201).json({success: true, message: 'Passenger Added'});
})
.error(function(err){
console.log(err);
res.status(500).json({success: false, message: 'Something broke'});
});
})
.error(function(err){
console.log(err);
res.status(500).json({success: false, message: 'Something bigger broke'});
});
});
var server = app.listen(4000, function(){
console.log('Wedding Time!');
});