-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
117 lines (100 loc) · 3.84 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
// // **************************************************************************************************
// Setup and configuration **************************************************************************
// **************************************************************************************************
// Module dependencies //////////////////////////
var dotenv = require('dotenv').config(), // set environment variables (e.g., production environment). must be done before creating express app!
express = require('express'),
app = express(),
// bodyParser = require("body-parser"), // deprecated in current express version
server = require('http').createServer(app),
io = require('socket.io')(server),
fs = require('fs-extra'),
logger = require('winston'),
Session = require('express-session'),
LokiStore = require('connect-loki')(Session), // session store
// Custom server modules
Controller = require('./serverModules/Controller'),
config = require('./config.json');
// Enable cross-origin resource sharing
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// enable POST submissions (with JSON encoded body)
//app.use(bodyParser.urlencoded()); // deprecated
app.use(express.urlencoded({ extended: true }));
app.use(express.json({limit: "2mb"})); // default limit is 100kb, but action logs might be larger
// handle JSON parse errors
app.use((error, req, res, next) => {
if (error instanceof SyntaxError) {
res.send("SyntaxError: " + error.type);
} else {
next();
}
});
// use sessions (to remember that a user is already logged in)
var session = Session({
store: new LokiStore({}),
secret: "8lacmxjk",
resave: true,
saveUninitialized: true,
});
// make request object (which contains the session) available for websocket handler (because authentication is done over websocket)
io.use((socket, next) => {
session(socket.request, socket.request.res, next);
});
app.use(session);
// Template engine
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
// log HTTP requests on the console
//app.use(express.logger('dev'));
// serve static content
app.use(express.static(__dirname + '/public'));
// make sure that required directories exist
if (!fs.existsSync("log")){
fs.mkdirSync("log");
console.log("creating directory 'log'");
}
if (!fs.existsSync("database")){
fs.mkdirSync("database");
console.log("creating directory 'database'");
}
if (!fs.existsSync("public/images/logos/upload/")){
fs.mkdirSync("public/images/logos/upload/");
console.log("creating directory 'public/images/logos/upload/'");
}
// initialize logging
// logging levels:
// error: 0, -> console, vbslog, errorlog
// warn: 1, -> console, vbslog
// info: 2, -> console, vbslog
// verbose: 3, -> vbslog
// debug: 4,
// silly: 5
logger.configure({
transports: [
new (logger.transports.Console)({
level: "info"}),
new (logger.transports.File)({
name: "infoLog",
filename: "log/vbsLog.log",
level: "info"}),
new (logger.transports.File)({
name: "fullLog",
filename: "log/vbsFullLog.log",
level: "verbose",
handleExceptions: true}),
new (logger.transports.File)({
name: "errorLog",
filename: "log/vbsErrorLog.log",
level: "error",
handleExceptions: true})
]
});
logger.info("RESTARTING SERVER on port " + config.server.port);
// Controller is a singleton object that handles all the application logic
// it can be required from any module
Controller.init(app, io);
server.listen(config.server.port);