forked from chemdemo/nodeblog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
80 lines (69 loc) · 2.51 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Blog system build with Node.js
* @Author: dmyang <[email protected]>
*/
var express = require('express');
var http = require('http');
var path = require('path');
var swig = require('swig');
var filters = require('./server/utils/filters');
var connectDomain = require('connect-domain');
var RedisStore = require('connect-redis')(express);
var staticDir = __dirname + '/web';
var settings = require('./settings');
var maxAge = settings.EXPIRES;
var routes = require('./server/routes');
var app = express();
app.configure('development', function() {
app.use(express.static(staticDir));
});
app.configure('production', function() {
app.use(express.static(staticDir, {maxAge: maxAge}));
app.set('view cache', true);
});
app.enable('trust proxy');
app.set('port', process.env.PORT || settings.APP_PORT);
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/server/views');
swig.setFilter('split', filters.split);
swig.setFilter('length', filters.length);
swig.setFilter('genLink', filters.genLink);
swig.setFilter('countFormat', filters.countFormat);
swig.setFilter('toString', filters.toString);
app.use(express.favicon(__dirname + '/web/favicon.ico'));
app.use('/upload/', express.static(settings.UPLOAD_DIR, {maxAge: maxAge}));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser(settings.COOKIE_SECRET));
// debug on win platform
// app.use(express.session({secret: settings.SESSION_SECRET}));
app.use(express.session({
store: new RedisStore({
host: settings.SESSION_HOST,
port: settings.SESSION_PORT
}),
secret: settings.COOKIE_SECRET
}));
app.use(express.csrf());
app.use(function(req, res, next) {
res.locals.token = req.session ? req.session._csrf : '';
res.locals.env = app.settings.env;
next();
});
app.use(connectDomain());
app.use(app.router);
routes(app);
app.use(function(err, req, res, next) {
var env = process.env.NODE_ENV || 'development';
if(err.status === 404) return res.render('404');
console.error('Error occurs(' + env + ' mode):\n', err.stack, '\nDate: ' + new Date());
res.render('error', {error: err.message || 'Unknown error!'});
});
process.on('uncaughtException', function(err) {
console.log('uncaughtException err: %s, at %s', err, new Date());
});
http.createServer(app).listen(app.get('port'), function() {
console.log("Application listening on port %s in %s mode, pid: %s.", app.get('port'), app.settings.env, process.pid);
});