-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathup.js
59 lines (45 loc) · 1.53 KB
/
up.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
var http = require('http');
var https = require('https');
const fs = require('fs')
http.createServer(onRequest).listen(3000);
function onRequest(client_req, client_res) {
const localPath=`.${client_req.url}`.split('?')[0];
if (fs.existsSync(localPath)) {
console.log('serve: ' + client_req.url + `(${localPath})`);
const contentTypes={
'html': 'text/html',
'js': 'application/javascript',
'css': 'text/css',
'json': 'application/json',
'svg': 'image/svg+xml',
}
let contentType='text/plain';
const ext=localPath.split('.')[2];
if (ext && contentTypes[ext]) {
contentType=contentTypes[ext];
}
const body=fs.readFileSync(localPath)
client_res.setHeader('Content-Type', contentType);
client_res.writeHead(200);
client_res.end(body);
} else {
console.log('serve: ' + client_req.url + `(remote)`);
const options = {
hostname: process.argv[2],
path: client_req.url,
port: 443,
method: client_req.method,
headers: client_req.headers
};
options.headers.host=options.hostname;
var proxy = https.request(options, function (res) {
client_res.writeHead(res.statusCode, res.headers)
res.pipe(client_res, {
end: true
});
});
client_req.pipe(proxy, {
end: true
});
}
}