app.js
2.8 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// # config - app
var express = require('express')
var flash = require('connect-flash')
var dynamicHelpers = require('./dynamic-helpers')
var development = require('./development')
var production = require('./production')
var passport = require('passport')
var settings = require('./settings')
var routes = require('./routes')
var ioc = require('electrolyte')
var session = require('express-session')
var methodOverride = require('method-override')
var errorHandler = require('errorhandler')
var serveFavicon = require('serve-favicon')
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')
var dataNode = require('../lib/data-node')
var helmet = require('helmet')
module.exports = function(lib, callback) {
var app = express()
// set the environment
app.set('env', lib.config.env)
// disable x-powered-by
app.disable('x-powered-by')
// trust proxy
app.enable('trust proxy')
// set-up electrolyte
ioc.loader('controllers', ioc.node('app/controllers'));
ioc.loader('models', ioc.node('app/models'));
ioc.loader(ioc.node('config'));
// set-up data nodes (inject data)
ioc.loader(dataNode(lib, 'lib'));
ioc.loader(dataNode(app, 'app'));
// set the default views directory
app.set('views', lib.config.viewsDir)
// make jade the default view engine
app.set('view engine', 'jade')
// make jade pretty
app.locals.pretty = true
// use helmet for security
app.use(helmet.defaults())
// ignore GET /favicon.ico
app.use(serveFavicon(lib.config.favicon))
// environment configs
switch(lib.config.env){
case 'development':
development(lib, app)
break
case 'production':
production(lib, app)
break
}
// pass a secret to cookieParser() for signed cookies
app.use(cookieParser(lib.config.cookieParser))
// parse request bodies
app.use(bodyParser())
// support _method (PUT in forms etc)
app.use(methodOverride())
// add req.session cookie support
app.use(session(lib.config.session))
// add flash message support
app.use(flash())
// add dynamic helpers for views
app.use(function(req, res, next) {
res.locals.req = req
res.locals.messages = {
success: req.flash('success'),
error: req.flash('error'),
info: req.flash('info'),
warning: req.flash('warning')
}
next()
})
// add support for authentication
app.use(passport.initialize())
app.use(passport.session())
passport.use(lib.db.model('User').createStrategy())
passport.serializeUser(lib.db.model('User').serializeUser())
passport.deserializeUser(lib.db.model('User').deserializeUser())
// load the routes
routes(lib, app)()
// static server
app.use(express.static(lib.config.publicDir, lib.config.staticServer))
// error handling
app.use(errorHandler())
callback(null, app)
return {
app: app,
lib: lib
}
}