forked from NikhilM98/sugarizer-school-portal-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (57 loc) · 1.76 KB
/
index.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
// require files
var express = require('express'),
http = require('http'),
https = require('https'),
fs = require('fs'),
process = require('process'),
settings = require('./config/settings'),
wait4db = require('./config/wait4db'),
common = require('./helper/common'),
ini = settings.load(),
app = express(),
server = null;
// Prevent uncaught exception
process.on("uncaughtException", function(err) {
console.error("FATAL ERROR, uncaught exception '" + err.message + "'");
console.error(err.stack);
process.exit(-1);
});
// wait for database
wait4db.waitConnection(ini, function(db) {
if (!db) {
console.log("Cannot connect with the database");
process.exit(-1);
}
// init common
common.init(ini);
//configure app setting
require('./config/main')(app, ini);
// include api routes
require('./api/route')(app, ini, db);
// include dashboard routes
require('./dashboard/route')(app, ini);
// Handle https
if (ini.security.https) {
var credentials = common.loadCredentials(ini);
if (!credentials) {
console.log("Error reading HTTPS credentials");
process.exit(-1);
}
server = https.createServer(credentials, app);
} else {
server = http.createServer(app);
}
// Start listening
var info = JSON.parse(fs.readFileSync("./package.json", 'utf-8'));
console.log(info.description+" v"+info.version);
console.log("node v"+process.versions.node);
console.log("Settings file './env/config.ini'");
server.listen(ini.web.port, function() {
console.log("Server is listening on"+(ini.security.https ? " secure":"")+" port " + ini.web.port + "...");
}).on('error', function(err) {
console.log("Ooops! cannot launch Server on port "+ ini.web.port+", error code "+err.code);
process.exit(-1);
});
});
//export app for testing
module.exports = app;