-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
76 lines (70 loc) · 2.2 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
#!/usr/bin/env node
//Modules
var config = require('./config');
process.env.TZ = config.timezone;
var http = require('http');
var path = require('path');
var express = require('express');
var hbs = require('express-hbs');
var flash = require('connect-flash');
var helpers = require('./lib/helpers');
var routes = require('./routes');
var app = module.exports = express();
//Helpers
hbs.registerHelper('dateFormat', helpers.dateFormat);
hbs.registerHelper('timeFormat', helpers.timeFormat);
hbs.registerHelper('miniImg', helpers.miniImg);
hbs.registerHelper('newsDigest', helpers.newsDigest);
hbs.registerHelper('imageEntry2Html', helpers.imageEntry2Html);
hbs.registerHelper('urlEncode', helpers.urlEncode);
hbs.registerHelper('tags2sitemap', helpers.tags2sitemap);
hbs.registerHelper('pagination', helpers.pagination);
//App config
app.set('port', config.port);
app.engine('hbs', hbs.express3({
defaultLayout: __dirname + '/views/layout.hbs',
partialsDir: __dirname + '/views/partials'
}));
app.set('view engine', 'hbs');
app.set('views', __dirname + '/views');
app.use(express.compress());
app.use(express.favicon(path.join(__dirname, 'public/favicon.ico')));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser(config.cookieSecret));
app.use(express.session());
app.use(express.csrf());
app.use(function (req, res, next) {
res.locals.token = req.session ? req.session._csrf : '';
res.locals.session = req.session;
next();
});
app.use(flash());
app.use(app.router);
app.use(express.compress());
app.use(express.static(path.join(__dirname, 'public'), {maxAge: config.staticMaxAge}));
app.disable('x-powered-by');
app.set('siteName', config.siteName);
// 404
app.use(function(req, res, next){
res.status(404);
res.render('404');
});
// For development
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// For production
if ('production' == app.get('env')) {
app.use(function(err, req, res, next){
res.status(err.status || 500);
res.render('500');
});
}
// Routes
routes(app);
if (require.main === module) {
http.createServer(app).listen(app.get('port'), function(){
console.log("Express Start app.js at http://127.0.0.1:" + app.get('port'));
});
}