forked from Rahul-Barick/diary-application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diary-app.js
75 lines (63 loc) · 2.31 KB
/
diary-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
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function (worker, code, signal) {
console.log('worker ' + worker.process.pid + 'died');
process.exit(1);
});
} else {
var express = require('express');
var app = express();
var config = require('./configs/config.js');
var http_status = require('./helpers/http_status.js');
var bodyParser = require('body-parser');
var routes = require('./routes');
var session = require('express-session');
var mongo_store = require('connect-mongo')(session);
// ENV
console.log("ENV --> " + config.env);
// Mongo Connection
require('./helpers/connect_mongo.js').connect_mongo(function () {
// parse form url encoded
app.use(bodyParser.urlencoded({ limit: '512kb', extended: false }));
//set views folder
app.set('views', './assets/views');
app.set('view engine', 'pug');
// // Static Contents
app.use('/css', express.static('./assets/css'));
app.use('/js', express.static('./assets/js'));
//session middleware
app.use(session({
store: new mongo_store({ db: require('./helpers/connect_mongo.js').get_mongo_connection() }),
secret: config[config.env].session_secret,
resave: false,
saveUninitialized: true
}));
// Locals
app.use(function (req, res, next) {
res.locals.base_url = config[config.env].base_url;
if (req.session && req.session.username) {
res.locals.sess_username = req.session.username;
}
next();
});
// API Routes
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
http_status.NOT_FOUND(res);
});
// error handler
app.use(function (err, req, res, next) {
http_status.INTERNAL_SERVER_ERROR(res, { message: err.message });
});
app.listen(config[config.env].PORT, () => {
console.log(`Server listening on Port ${config[config.env].PORT}`);
});
});
module.exports = app;
}