This repository has been archived by the owner on Apr 4, 2018. It is now read-only.
/
explorer.js
87 lines (73 loc) · 1.62 KB
/
explorer.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
var http = require('http');
var fs = require('fs');
var explorer = {};
/*
* serve html file
*/
explorer.html = function (req, rsp, name, statusCode) {
var content = fs.readFileSync("public/" + name + ".html");
rsp.writeHead(statusCode || 200, {'Content-Type': 'text/html; charset=utf-8'});
rsp.end(content);
};
/*
* serve JSON content
*/
explorer.json = function (req, rsp, content) {
rsp.writeHead(200, {'Content-Type': 'application/json'});
rsp.end(JSON.stringify(content));
}
/*
* use redirector to present mapping information as json
*/
explorer.head = function (req, rsp, path, info) {
var options = {
'method': 'HEAD',
'host': info.redirector,
'path': path,
'headers': {
'host': info.upstream
}
};
var red = http.request(options, function(res) {
var head = {
'options': options,
'headers': res.headers,
'statusCode': res.statusCode,
'location': res.headers.location
};
explorer.json(req, rsp, head);
})
red.end();
};
/*
* explorer
*/
explorer.request = function (req, rsp, path, info) {
/*
* serve the single-page app
*/
if (path.match(/^\/$/)) {
return explorer.html(req, rsp, 'index');
}
if (path.match(/^\/info.json$/)) {
return explorer.json(req, rsp, info);
}
/*
* proxy the redirector
*/
if (path.match(/^\/head\//)) {
path = path.replace(/^\/head/, "");
return explorer.head(req, rsp, path, info);
}
/*
* error pages
*/
if (path.match(/^\/410$/)) {
return explorer.html(req, rsp, '410');
}
if (path.match(/^\/418$/)) {
return explorer.html(req, rsp, '418');
}
return explorer.html(req, rsp, '404', 404);
}
module.exports = explorer;