-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
111 lines (86 loc) · 2.83 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
/**
* This sample app show-cases the `NoGap` framework renders a simple web page.
* We use Angular + Bootstrap + Font-Aweseom, but none of that is required to use NoGap components.
*/
"use strict";
const process = require('process');
process.on('uncaughtException', function (err) {
console.error('[UNCAUGHT ERROR]');
console.error(err.stack);
});
global.Promise = require('bluebird');
// load express
var express = require('express');
var cookieParser = require('cookie-parser');
var session = require('express-session');
// load squishy
require('squishy');
// add shutdown handler
require('./lib/maintenance');
// load config
var appConfig = require('./appConfig');
// setup logging
require('./lib/logging');
// Warming up...
console.log('Starting server. Please wait...');
// setup express + session & cookie management
// Note: The default implementations for components bootstrapping and transport
// require an express application object, as well as session mnagement, to work.
var app;
(function setupExpress() {
// start express
app = express();
app.use(cookieParser()); // required for `express-session` module
app.set('title', 'NoGap Test App'); // set title
// install session manager:
app.use(session({
// secret token
secret: 'mySuperCoolSecret123qwerty',
resave: true,
saveUninitialized: true,
// default cookie settings
cookie: {
path: '/',
httpOnly: true
},
// We are not using a store, so our session won't persist through server re-starts.
// store: ????
}));
})();
(function installNoGap() {
// Default way of including `NoGap`:
var NoGap = require('nogap');
// initialize NoGap
return NoGap.Loader.start(app, appConfig.nogap);
})().then(() => {
// fall back handler for unhandled routes:
app.use(function(req, res, next) {
err = new Error('Not Found: ' + req.originalUrl);
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
var status = err.status || 500;
console.warn('Invalid request (' + status + '): ' + err.stack || err);
res.writeHead(status, {'Content-Type': 'text/html'});
if (appConfig.dev) {
res.write('<pre>' + err.stack + '</pre>');
}
res.end();
});
// start HTTP server:
(function startServer() {
app.set('port', appConfig.httpd.port || 8080);
try {
app.serverInstance = app.listen(app.get('port'), function() {
console.log('Test app is now up and running at port ' + app.serverInstance.address().port);
});
}
catch (err) {
// exit on error
process.exit(err);
}
})();
});
module.exports = app;