-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
64 lines (51 loc) · 1.64 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
var express = require('express');
var path = require('path');
var passport = require('passport');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var flash = require('connect-flash');
var KnexSessionStore = require('connect-session-knex')(session);
var expressLayouts = require('express-ejs-layouts');
var validator = require('express-validator');
var db = require('./config/database');
require('./config/passport')(passport);
// express
var app = express();
// view engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'app', 'views'));
// add layout support
app.use(expressLayouts);
app.set('layout', 'layouts/layout');
// read cookies (needed for auth)
app.use(cookieParser());
// get information from html forms
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// form validator
app.use(validator());
// set the folder for static assets
app.use(express.static(path.join(__dirname, 'app', 'public')));
// store session in database (needed for auth)
var store = new KnexSessionStore({
knex: db,
tablename: 'sessions'
});
// required for passport (needed for auth)
app.use(session({
secret: process.env.SESSION_SECRET,
store: store
}));
app.use(passport.initialize());
// persistent login sessions
app.use(passport.session());
// use connect-flash for flash messages stored in session
app.use(flash());
// routes
require('./app/routes')(app, passport);
require('./app/routes_admin')(app, passport);
// start server
app.listen(process.env.PORT, () => {
console.log('server started on port ' + process.env.PORT);
});