-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrap.js
97 lines (72 loc) · 2.74 KB
/
bootstrap.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
module.exports = function(app, express) {
var exphbs = require('express-handlebars');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var fs = require('fs');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var config = require("./config/database");
// import routes to be used
var index = require('./routes/index');
var attendees = require('./routes/attendees');
var port = process.env.PORT || 8000;
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.set('trust proxy', 'loopback');
// setup the logger and only log errors
var accessLogStream = fs.createWriteStream('hackathon.log',
{ flags: 'a' });
app.use(logger('combined', { stream: accessLogStream,
skip: function (req, res) { return res.statusCode < 400 } }));
app.use(favicon(__dirname + '/assets/images/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser('session_secret'));
app.use(express.static(path.join(__dirname, 'assets')));
// middleware to use for all requests
app.all("*", function(req, res, next) {
console.log('request being processed');
next();
});
// static pages routes
app.get('/', index.index);
app.get('/register', index.apply);
app.get('/schedule', index.schedule);
app.get('/contact', index.contact);
app.get('/guide', index.guide);
app.get('/terms', index.terms);
app.get('/privacy', index.privacy);
app.get('/rules', index.rules);
app.get('/thankyou', index.thankyou);
// API
app.post('/addAttendee', attendees.addAttendee);
app.get('/verify/:email/:code', attendees.processCode);
// 404 error handler
app.get('*', function(req, res) {
res.render('404', 404);
});
// 500 error handler --> production only
// need to improve 500 logging
app.use(function(error, req, res, next) {
res.status(500);
res.render('500', { title:'500: Internal Server Error', error: error });
});
// ensure request is application/json
function ensureJSON(req, res, next) {
if ( ! req.is('application/json') ) {
res.status(400).json({ 'Error': 'Bad Request' });
} return next();
}
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
} res.redirect('/login');
}
module.exports = app;
app.listen(port);
console.log('Magic happens on port ' + port);
}