-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathserver.js
100 lines (90 loc) · 3.26 KB
/
server.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
var config = require('./bin/config.js'),
fs = require('fs'),
http = require('http'),
HttpHashRouter = require('http-hash-router'),
st = require('st'),
request = require('request'),
sendJson = require('send-data/json'),
sendHtml = require('send-data/html'),
sendError = require('send-data/error')
var router = HttpHashRouter();
var backend_path = config.get('backend_path'),
frontend_path= config.get('frontend_path'),
backend_host = config.get('backend_host');
var backend_proxy = function (req, res, opts, cb) {
if(!backend_host){
console.error(config.get('backend_config_error'));
sendJson(req, res, config.get('backend_config_error'));
}else{
// Strip our Backend path prefix before proxying the request:
req.pipe(request('http://'+backend_host+req.url.substring(backend_path.length))).pipe(res);
}
}
var index_page = function (req, res, opts, cb) {
var index_html = fs.readFileSync(__dirname + '/index.html');
if(backend_path !== '/ws'){
index_html = index_html.toString().replace('// BACKEND_PATH_CONFIG', 'window.backend_path = \''+backend_path+'\';');
}
sendHtml(req, res, {
body: index_html,
statusCode: 200
})
};
var ico = fs.readFileSync(__dirname + '/favicon.ico');
var favicon = function (req, res, opts, cb) { res.end(ico); };
// Routes
router.set('/favicon.ico', favicon);
router.set(frontend_path, index_page);
router.set(backend_path+'*', backend_proxy);
// Ensure that we return *something* on the default path
if( frontend_path !== '/'){
router.set('/', function (req, res, opts, cb) {
sendHtml(req, res, {
body: config.get('path_info'),
statusCode: 200
})
});
// append trailing slash - ensure a valid relative path for static assets
router.set(config.get('no_slash_frontend'), function (req, res, opts, cb) {
res.statusCode=302;
res.setHeader('Location', frontend_path );
res.end();
});
}
// Serve static assets from our static_content folders
var static_content=['assets','node_modules'];
for(var folder in static_content){
router.set(frontend_path+static_content[folder]+"/*", st({
path: static_content[folder], url: frontend_path+static_content[folder]
}));
}
// TODO: these two functions are just for testing, can be deleted:
router.set(frontend_path+'status', function (req, res, opts, cb) {
sendJson(req, res, {'status': 'ok'})
});
router.set(frontend_path+'hostname', function (req, res, opts, cb) {
var data = '<p>Hostname: ' + config.get('HOSTNAME') + '</p>';
sendHtml(req, res, {
body: data,
statusCode: 200
})
});
// Listen
var server = http.createServer(function handler(req, res) {
router(req, res, {}, onError);
function onError(err) {
if (err) {
res.statusCode = err.statusCode || 500;
res.end(err.message);
}
}
});
server.listen(config.get('PORT'), config.get('IP'), function () {
console.log( 'Listening on ' + config.get('IP') + ', port ' + config.get('PORT') )
console.log( config.get('path_info') );
if( !backend_host ){
console.error(config.get('backend_config_error'));
}else{
console.log(config.get('backend_config_info'));
}
});