app.js
2.3 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
// # app
var express = require('express')
var flash = require('connect-flash')
var dynamicHelpers = require('./dynamic-helpers')
var development = require('./development')
var production = require('./production')
var auth = require('./auth')
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/dataNode')
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
// ignore GET /favicon.ico
app.use(serveFavicon(lib.config.favicon))
// development only config
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(dynamicHelpers)
// add support for authentication
auth(lib, app)()
// 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}
}