This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
96 lines (81 loc) · 2.54 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
/******************************************************************************
project: dicanio
file: server.js
main entry point for app
******************************************************************************/
// jshint node: true, esversion: 6
'use strict';
const express = require('express'),
app = express(),
pkg = require('./package.json'),
bp = require('body-parser'),
expressSession = require('express-session'),
excon = require('express-controller'),
flash = require('connect-flash'),
logger = require('./logs'),
models = require('./models'),
moment = require('moment'),
bars = require('express-handlebars');
const router = express.Router();
// handlebars as templating engine
var hbs = bars.create({
defaultLayout: 'layout',
extname: '.hbs',
helpers: {
killerLives: (lives, lost) => {
if ((lives - lost) <= 0) {
return '<span>☠</span>';
} else {
let heart = '<span>♥</span>';
let lostheart = '<span class="lost">♥</span>'
if (lost) {
return heart.repeat(lives - 1) + lostheart;
} else {
return heart.repeat(lives);
}
}
}
}
});
app.engine('.hbs', hbs.engine);
app.set('view engine', '.hbs');
// set static route
app.use(express.static('assets'));
// body-parsing for post requests
app.use(bp.urlencoded({ 'extended': true }));
app.use(bp.json());
app.set('port', process.env.PORT || 2000);
// set up middleware
app.use(expressSession({
secret: 'dfTJ2gIw6wfw',
resave: false,
saveUninitialized: false
}));
app.use(flash());
app.use((req, res, next) => {
res.locals.flash_success = req.flash('success');
res.locals.flash_error = req.flash('error');
res.locals.flash_info = req.flash('info');
res.locals.dev = process.env.NODE_ENV || 'dev';
next();
});
// authentication using passport.js
require('./auth')(app);
// add routing
app.use(router);
require('./routes')(app);
excon.setDirectory(__dirname + '/controllers').bind(router);
app.use((req, res) => {
res.status(400).render('errors/404', {
title: 'Uh-oh!'
});
});
app.locals.pkg = pkg;
//set up sequelize and start server listening
models.sequelize.sync().then( () => {
console.log('Sequelize initialised at', moment().format('HH:mm:ss ddd'));
const server = app.listen(app.get('port'), () => {
console.log(pkg.name, 'running on port', server.address().port);
module.exports = server;
});
});