-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·118 lines (98 loc) · 3.01 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
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
/**
* Module dependencies.
*/
var express = require('express')
, fs = require('fs')
, util = require('util')
, config = require('./config')
, ejs = require('ejs')
, log4js = require('log4js')
, app = module.exports = express.createServer();
//set view engine
app.set('views', __dirname + '/views');
app.register('html', ejs );
app.set('view engine', 'html');
//app.enabled('view cache');
//app.enabled('case sensitive routes');
//app.enabled('strict routing');
// middleware
app.configure('development', function(){
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(express.logger('dev'));
});
app.configure('production', function(){
var oneYear = 31557600000;
app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
app.use(express.errorHandler());
});
app.configure(function(){
app.use(express.bodyParser({uploadDir: __dirname + '/data/uploads'}));
app.use(express.methodOverride());
app.use(express.cookieParser('keyboard cat'));
app.use(express.session({ secret: 'keyboard cat' }));
app.use(app.router);
app.use(express.favicon(__dirname + '/public/images/favicon.ico'));
});
// Locals
app.locals(config.globals);
app.locals({
title: config.globals.web_name
});
/* for 3.x
app.locals.use(function(req, res) {
res.locals.error = req.session.error || '';
res.locals.message = req.session.message || '';
delete req.session.error;
delete req.session.message;
});
*/
// set default layout, usually "layout"
app.set("view options", { layout: "layouts/admin" });
// set error handler
app.use(function(err, req, res, next) {
console.error(err);
res.render('500');
});
app.use(function(req, res) {
res.render('404');
});
// load all routes
var routesDir = "./routes/";
fs.readdir(routesDir, function(err, files) {
if (err) throw err;
files.forEach(function(file) {
if( /\w+\.js/.test(file) == true ) {
require(routesDir + file)(app);
}
});
});
// init database connection
var dbcfg = config.db.localhost;
var conn = util.format("mongodb://%s:%s@%s:%s/%s",
dbcfg.auth.username,
dbcfg.auth.password,
dbcfg.host,
dbcfg.port,
dbcfg.name);
mongoose = require("mongoose");
mongoose.connect(conn);
//load global util tool
utils = require('./libs/utils');
//init logger
utils.logfile = __dirname + '/data/logs/mama.log';
//log4js.addAppender(log4js.consoleAppender());
//log4js.addAppender(log4js.fileAppender(utils.logfile), 'mama');
logger = log4js.getLogger("mama");
logger.setLevel('DEBUG');
//init some global params
utils.readmeFile = __dirname + '/data/readme.txt';
utils.config = config;
//start web server
if (!module.parent) {
app.listen(8888);
logger.debug('Express started on port 8888');
}
process.on('uncaughtException', function (err) {
logger.error(err);
});