-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxreader.js
More file actions
77 lines (67 loc) · 2.46 KB
/
Copy pathmaxreader.js
File metadata and controls
77 lines (67 loc) · 2.46 KB
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
const http = require('http');
const url = require('url');
const request = require('request');
const fs = require('fs');
const commander = require('commander');
const hostname = 'localhost';
const port = 3003;
const os = require('os');
const osHostname = os.hostname()||'localhost';
commander.option('-s, --server <url>','Server 1S URL and port')
.option('-u, --user <user>','Server 1S user name')
.option('-p, --password <password>','Server 1S password')
commander.parse(process.argv);
const options = commander.opts();
const serverUrl = options.server||osHostname;
const serverUser = options.user;
const serverPass = options.password;
const server = http.createServer((req, res) => {
const baseURL = 'http://' + req.headers.host + '/';
const reqUrl = new URL(req.url,baseURL);
let requestUrl = reqUrl.searchParams.get('url');
const getServerUrlFromCLI = reqUrl.searchParams.get('cli');
reqUrl.searchParams.delete('url');
reqUrl.searchParams.delete('cli');
if (getServerUrlFromCLI){
requestUrl = serverUrl + '?' + reqUrl.searchParams.toString() + '&hostname=' + osHostname;
}
else if (requestUrl) {
requestUrl += '&' + reqUrl.searchParams.toString() + '&hostname=' + osHostname;
}
if (requestUrl){
if (requestUrl.search(/^http:\/\//) < 0){
requestUrl = 'http://' + requestUrl;
}
console.log(requestUrl);
const reqConfig = {
json: true,
}
if (serverUser){
reqConfig.auth = {
user: serverUser,
pass: serverPass,
}
}
request(requestUrl,
reqConfig,
(perr, pres, pbody) => {
if (perr) { return console.log(perr); }
res.statusCode = pres.statusCode;
if (res.statusCode < 400){
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(pbody));
}
}
);
}
else {
const pathToFile = reqUrl.pathname === '/'? __dirname + '/index.html': __dirname + reqUrl.pathname;
const contentType = reqUrl.pathname.search(/\.css$/) >= 0 ? "text/css" : "text/html";
console.log(contentType);
res.writeHead(200, { 'content-type': contentType })
fs.createReadStream(pathToFile).pipe(res)
}
});
server.listen(port, () => {
console.log(`Maxreader running at http://${hostname}:${port}/`);
});