-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
103 lines (83 loc) · 2.31 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
95
96
97
98
99
100
101
102
103
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const passport = require('passport');
const session = require('express-session');
const flash = require('express-flash');
const compressoion = require('compression');
const logger = require('morgan');
const methodOverride = require('method-override');
// Load Keys
const keys = require('./config/keys');
// Load Routes
const index = require('./routes/index');
const user = require('./routes/user');
const auth = require('./routes/auth');
const migration = require('./routes/migration');
const provider = require('./routes/provider');
// Load Passport
require('./config/passport');
const app = express();
// Load MongoDB
mongoose.connect(keys.mongoURI)
.then(() => console.log('MongoDB connected...'))
.catch(err => console.error(err));
// Handlebars
app.engine('handlebars', exphbs({
defaultLayout: 'main',
helpers: {
ifeq: (a, b, options) => {
if(a === b) {
return options.fn(this);
}
return options.inverse(this);
},
toJSON: (object) => {
return JSON.stringify(object);
}
}
}));
app.set('view engine', 'handlebars');
// Compression Init
app.use(compressoion());
// Logger Init
app.use(logger('dev'));
// Body Parser
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json());
// Method-Override Init
app.use(methodOverride('_method'));
// Express Validator
app.use(expressValidator());
// Session Express Init
app.use(session({
secret: keys.sessionSecret,
resave: true,
saveUninitialized: true
}));
// Flash Express Init
app.use(flash());
// Password as Middleware
app.use(passport.initialize());
app.use(passport.session());
// Map user info to res.locals
app.use(function (req, res, next) {
res.locals.user = req.user;
next();
})
// Static folder
app.use(express.static(path.join(__dirname, 'public')));
// Use routes
app.use('/', index);
app.use('/user', user);
app.use('/auth', auth);
app.use('/migration', migration);
app.use('/provider', provider);
// Port info
app.set('port', process.env.PORT || 5000);
app.listen(app.get('port'), () => {
console.log(`Server started on ${app.get('port')}`);
})