-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·134 lines (110 loc) · 2.69 KB
/
index.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* index.js
*/
// set application env
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var crypto = require('crypto');
var express = require('express');
var hbs = require('express-hbs');
var log4js = require('log4js');
var _ = require('lodash');
// config
var config = require('./config');
// helpers
var helpers = require('./helpers');
// routes
var routes = require('./routes');
// errors
var errors = require('./errors');
// json
var packageInfo = require('./package.json');
// express
var app = express();
var appConfig = config();
/**
* midware
*/
var static = appConfig.paths.static;
var views = appConfig.paths.views;
// static assets
app.use(express.static(static));
// favicon
app.use(express.favicon(static + 'favicon.ico'));
// bodyParser
app.use(express.bodyParser());
// cookieParser
app.use(express.cookieParser());
// session
app.use(express.session({
secret: "node-boilerplate"
}));
// logger
app.use(express.logger());
/**
* env
*/
app.set('env', process.env.NODE_ENV);
/**
* views
*/
app.engine('hbs', hbs.express3({
partialsDir: views + '/partials'
}));
app.set('view engine', 'hbs');
app.set('views', views);
/**
* helper
*/
var assetHash = (crypto.createHash('md5')
.update(packageInfo.version + Date.now())
.digest('hex'))
.substring(0, 10);
helpers.loadCoreHelpers(null, assetHash);
// filter login
app.use('/admin', function(req,res,next) {
if (!_.isEmpty(req.session) && req.session.user) {
if (req.session.user.username == config().user.username &&
req.session.user.password == config().user.password) {
return next();
}
}
res.redirect('/login');
});
// handle routes
routes.frontend(app);
routes.admin(app);
// development env
if ('development' == app.get('env')) {
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
}
// production env
if ('production' == app.get('env')) {
// 404
app.use(function handleNotFound(req, res, next) {
errors.error404(req, res, next);
});
// 500
app.use(function handlerError(err, req, res, next) {
errors.error500(err, req, res, next);
});
// view cache
app.set('view cache', true);
// logger support
log4js.configure({
appenders: [{
type: 'console'
}]
});
logger = log4js.getLogger('normal');
logger.setLevel('INFO');
app.use(log4js.connectLogger(logger, {
level: log4js.levels.INFO
}));
}
// bootstrap server
app.listen(appConfig.server.port, appConfig.server.host, function() {
console.log('Express server listening on port ' + appConfig.server.port);
});