-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (91 loc) · 3.27 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
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
// Express framework
var express = require('express'),
// The app package
app = express(),
// An http server
server = require('http').createServer(app),
// Socket.io for live data
io = require('socket.io')(server),
// the body-parser for form processing
bodyParser = require('body-parser'),
// Get the async module to do async response
async = require('async'),
// Let's compress our http output
compression = require('compression'),
// Get all the environment vars from the env.json library
env = require(__dirname + "/env.json"),
// The static web folder for holding content
staticFolder = __dirname + '/' + (env.DIST_FOLDER || 'app'),
// The security key to post to secure endpoints
securityKey = env.SECURITYKEY || 'ChangeMe2SomethingNew',
// A basic security model
basic_auth = function (req, res, next) {
if (req.headers.authorization && req.headers.authorization.search('Basic ') === 0) {
// fetch login and password
if (new Buffer(req.headers.authorization.split(' ')[1], 'base64').toString() == 'admin:' + securityKey) {
next();
return;
}
}
console.log('Unable to authenticate user', req.headers.authorization);
res.header('WWW-Authenticate', 'Basic realm="Admin Area"');
if (req.headers.authorization) {
setTimeout(function () {
res.send('Authentication required', 401);
}, 5000);
} else {
res.send('Authentication required', 401);
}
},
// A collection of async routes to run on the server
appAsyncItems = [
// Root path for serving support files from site
express.static(staticFolder + '/html')
],
// This organizes the items which are incoming for async load
parallel = function (middlewares) {
return function (req, res, next) {
async.each(middlewares, function (mw, cb) {
mw(req, res, cb);
}, next);
};
},
// A compression helper
shouldCompress = function (req, res) {
if (req.headers['x-no-compression']) {
// don't compress responses with this request header
return false
}
// fallback to standard filter function
return compression.filter(req, res)
};
// Make the POST and PUT data available
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Root path for serving support files from site
console.log('starting static http server on html files');
app.use(express.static('app/html'));
// Allow Cross-Origin GET and POST requests from any host
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,x-no-compression Authorization');
next();
});
// Make the POST and PUT data available
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Start up compression on everything
app.use(compression({filter: shouldCompress}));
// Start up the async server item collection
app.use(parallel(appAsyncItems));
// @TODO Add Authentication middleware for user token creation
// @TODO Start Socket.io based middleware for interactions
// @TODO Look for interaction modules and expose them
// Start the server
console.log('listening on ' + env.PORT || 8080);
server.listen(env.PORT || 8080);