-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
128 lines (108 loc) · 3.73 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
119
120
121
122
123
124
125
126
127
128
'use strict';
var bodyParser = require('body-parser')
, config = require('./app/config')
, cookieParser = require('cookie-parser')
, express = require('express')
, fs = require('fs')
, http = require('http')
, https = require('https')
, morgan = require('morgan')
, serveStatic = require('serve-static')
, session = require('express-session')
, stylus = require('stylus');
// Loads up the file at the given path with the given charset. If the file does
// not exist, then an undefined is returned (instead of throwing an error). This
// process is synchronous.
function loadFileAsStringOrUndefined(filePath, charset) {
try {
fs.statSync(filePath, charset)
} catch (e) {
// an error will be thrown when the file does not exist so catch it
// and return undefined
return undefined;
}
return fs.readFileSync(filePath, charset).toString();
}
// Loads up the SSL credentials that can be used with the https module. If the key
// or certificate cannot be found, then an undefined is returned.
function loadSslCredentials() {
var privateKey = loadFileAsStringOrUndefined(config.privateKeyFilePath, 'utf8'),
certificate = loadFileAsStringOrUndefined(config.certificateFilePath, 'utf8');
if (!privateKey || !certificate) {
return undefined;
}
return {key: privateKey, cert: certificate}
}
// Starts the HTTP server with the given Express app
function startHttpServer(app) {
var httpServer;
try {
httpServer = http.createServer(app);
httpServer.on('error', httpError);
httpServer.listen(config.httpPort);
// if the app is being served through nginx, show the nginx information
// instead of the HTTP port for the app
if (process.env.NGINX_PORT) {
console.log('Server started: https://localhost:' + process.env.NGINX_PORT);
} else {
console.log('Server started on HTTP: ' + config.httpPort);
}
} catch (e) {
console.error('Error while starting HTTP server on ' + config.httpPort + ':');
console.error(e);
throw e;
}
}
// Starts the HTTPS server with the given Express app
function startHttpsServer(app) {
var httpServer;
var credentials = loadSslCredentials();
if (!credentials) { console.log('SSL issue'); }
try {
httpServer = https.createServer(credentials, app);
httpServer.on('error', httpError);
httpServer.listen(config.httpsPort);
console.log('Server started on HTTPS: ' + config.httpsPort);
} catch (e) {
console.error('Error while starting HTTPS server on ' + config.httpsPort + ':');
console.error(e);
throw e;
}
}
function httpError(exception, socket) {
console.error('An error occurred with the HTTP server:\n' + exception);
}
var app = express();
// use Pug view templates
app.set('views', __dirname + '/views');
app.set('view engine', 'pug');
// make JSON bodies and cookies available in req objects
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({
resave: false,
saveUninitialized: false,
secret: 'asdfasfdsafsdfsd'
}));
// use morgan for request logging
app.use(morgan('dev'));
// statically serve SLDS, the files in /public, and the babel-ified js in /lib
app.use('/libs/salesforce-ux/design-system', serveStatic(__dirname + '/node_modules/@salesforce-ux/design-system/assets'))
app.use('/libs/jquery', serveStatic(__dirname + '/node_modules/jquery/dist/'))
app.use(serveStatic(__dirname + '/public'));
app.use(serveStatic(__dirname + '/lib'));
// use routers from app/routers/index.js
app.use(require('./app/routers'));
process.on('uncaughtException', function(err) {
console.log(err);
process.exit(1);
});
// start the web server.
if (config.useHttps) {
startHttpsServer(app);
} else {
startHttpServer(app);
}