-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
187 lines (139 loc) · 4.37 KB
/
run.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var express = require('express');
var app = express();
//app.engine('jade', require('jade').__express);
//Register views folder
app.set('views', './app/views'); // specify the views directory
//Register template engine
app.set('view engine', 'jade'); // register the template engine
//Allow use static
app.use(express.static('app/public'));
//Allow download file
//app.use(express.static('app/files'));
//production or development
//process.env.NODE_ENV = 'production';
// ----------------------ERROR HANDLING
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);
//global.app = app;
function logErrors(err, req, res, next) {
console.error(err.stack);
next(err);
}
function clientErrorHandler(err, req, res, next) {
if (req.xhr) {
res.send(500, { error: 'Something blew up!' });
} else {
next(err);
}
}
function errorHandler(err, req, res, next) {
res.status(500);
res.render('error', { error: err });
}
// ------------------- END ERROR HANDLING
//Default is false
global.is_debug = false;
require('./app/tools/pre_init');
require('./app/tools/general');
var configs = require('./configs');
showLog( process.argv );
//nodejs run.js config_production.js
var config_file = process.argv[2] || "config.json";
showLog("Config file name is " + config_file);
configs( config_file, function(config){
global.config = config;
app.set('env', config.env || "development" );
global.is_debug = config.env == "developement";
showWarn("debug is ",is_debug );
if ( is_debug ){
//Show pretty html on the bebug case
app.locals.pretty = true;
}
require('./app/db/db_schemal');
connectDB();
});
function connectDB(){
var mongoose = require('mongoose');
mongoose.connect( config.mongo_uri );
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
showSucc("Connected to the db");
connectRedis(db);
});
}
/*
function connectDB(){
var MongoClient = require('mongodb').MongoClient,
format = require('util').format;
//Connect to database
MongoClient.connect( config.mongo_uri , function(err, db) {
if(err) throw err;
global.ObjectID = require('mongodb').ObjectID;
global.db = db;
showSucc("Connected to db with uri " + config.mongo_uri );
//if you don't wanna use redis, use boot() directly
connectRedis();
});
}*/
function connectRedis(db){
var redis = require("redis"),
client = redis.createClient();
client.on('error', function (err) {
showError("Redis error", err);
});
client.on('connect', function(){
showSucc("connected to redis");
//Boot router
boot(db);
});
}
function boot(db){
var expressSession = require('express-session');
var cookieParser = require('cookie-parser'); // the session is stored in a cookie, so we use this to parse it
var MongoStore = require('connect-mongo')(expressSession);
app.use(cookieParser());
//app.use(expressSession({secret:'a3flsjf&*&S*DHFSDF'}));
app.use(expressSession({
secret: 'tais#@@$%ao',
name: "cookie_name",
store:new MongoStore({
mongoose_connection: db
}),
proxy: true,
resave: true,
//cookie: { maxAge: 1000*60*2 },
saveUninitialized: true
}));
//Allow get body in the request
var bodyParser = require('body-parser');
//Limit is 1mb
app.use(bodyParser.urlencoded({ extended: false, limit: '1mb' }));
app.use( bodyParser.json( {limit: '1mb'}) ); // to support JSON-encoded bodies
/*app.use(bodyParser.json({limit: '1mb'}));
app.use(bodyParser.urlencoded({extended: false,limit: '1mb'}));*/
var methodOverride = require('method-override');
app.use(methodOverride());
if ( !global.is_debug ){
var csrf = require('csurf')
app.use(csrf())
// error handler
app.use(function (err, req, res, next) {
if (err.code !== 'EBADCSRFTOKEN') return next(err)
// handle CSRF token errors here
res.status(403)
res.send('session has expired or form tampered with')
})
}
require('./app/modules/frontend')(app);
require('./app/modules/backend')(app);
var listen_port = config.port || 3000;
var server = app.listen(listen_port, function() {
console.log('Listening on port %d', server.address().port);
});
//Custom 404 page
app.use(function(req, res, next){
res.status(404).send('Sorry cant find that!')
});
}