-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress.js
executable file
·103 lines (84 loc) · 2.69 KB
/
express.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
'use strict';
/**
* Module dependencies.
*/
var mean = require('meanio'),
compression = require('compression'),
morgan = require('morgan'),
consolidate = require('consolidate'),
cookieParser = require('cookie-parser'),
expressValidator = require('express-validator'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
assetmanager = require('assetmanager'),
session = require('express-session'),
mongoStore = require('connect-mongo')(session),
helpers = require('view-helpers'),
flash = require('connect-flash'),
config = mean.loadConfig();
module.exports = function(app, passport, db) {
app.set('showStackError', true);
// Prettify HTML
app.locals.pretty = true;
// cache=memory or swig dies in NODE_ENV=production
app.locals.cache = 'memory';
// Should be placed before express.static
// To ensure that all assets and data are compressed (utilize bandwidth)
app.use(compression({
// Levels are specified in a range of 0 to 9, where-as 0 is
// no compression and 9 is best compression, but slowest
level: 9
}));
// Only use logger for development environment
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
// assign the template engine to .html files
app.engine('html', consolidate[config.templateEngine]);
// set .html as the default extension
app.set('view engine', 'html');
// The cookieParser should be above session
app.use(cookieParser());
// Request body parsing middleware should be above methodOverride
app.use(expressValidator());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(methodOverride());
// Import the assets file and add to locals
var assets = assetmanager.process({
assets: require('./assets.json'),
debug: process.env.NODE_ENV !== 'production',
webroot: /public\/|packages\//g
});
// Add assets to local variables
app.use(function(req, res, next) {
res.locals.assets = assets;
mean.aggregated('js', 'header', function(data) {
res.locals.headerJs = data;
next();
});
});
// Express/Mongo session storage
app.use(session({
secret: config.sessionSecret,
store: new mongoStore({
db: db.connection.db,
collection: config.sessionCollection
}),
cookie: config.sessionCookie,
name: config.sessionName,
resave: true,
saveUninitialized: true
}));
// Dynamic helpers
app.use(helpers(config.app.name));
// Use passport session
app.use(passport.initialize());
app.use(passport.session());
//mean middleware from modules before routes
app.use(mean.chainware.before);
// Connect flash for flash messages
app.use(flash());
};