-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnode_server_old.js
124 lines (112 loc) · 3.39 KB
/
node_server_old.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
117
118
119
120
121
122
123
124
#!/usr/bin/env node
// @flow
'use strict';
const spawn = require('child_process').spawn;
const exec = require('child_process').execSync;
const http = require('http');
const util = require('util');
const path = require('path');
const fs = require('fs');
const port = 8080;
const host = `http://127.0.0.1:${port}`;
const serveDir = path.resolve(process.argv[2] || process.cwd());
const filesMimeTypesCache = {};
function getMimeType(filepath) {
if (!filesMimeTypesCache[filepath]) {
switch (path.extname(filepath)) {
case '.css':
filesMimeTypesCache[filepath] = 'text/css';
break;
case '.js':
filesMimeTypesCache[filepath] = 'application/javascript';
break;
case '.wasm':
filesMimeTypesCache[filepath] = 'application/wasm';
break;
default:
filesMimeTypesCache[filepath] = exec(
`file --mime-type --brief ${filepath}`
)
.toString()
.trim();
}
}
return filesMimeTypesCache[filepath];
}
function handler(req, res) {
const reqPath = req.url.replace(/\?.*/, '').replace(/_cb.*/, '');
const reqPathFSPath = path.join(serveDir, reqPath);
function errRes(err, code) {
console.log(`${code} ${req.url} ${err}`);
res.writeHead(code, {'Content-Type': 'text/plain'});
res.write(err.stack);
res.end();
}
// does the request point to a valid file or dir at all?
let reqPathStat = null;
try {
reqPathStat = fs.lstatSync(reqPathFSPath);
} catch (reqPathStatErr) {
// nothing there
return errRes(reqPathStatErr, 404);
}
try {
// return file or index file contents
const filepath = reqPathStat.isDirectory()
? path.join(reqPathFSPath, 'index.html')
: reqPathFSPath;
const mimeType = getMimeType(filepath);
const contents = fs.readFileSync(filepath);
console.log(`200 ${req.url} ${mimeType}`);
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
res.writeHead(200, {'Content-Type': mimeType});
res.write(contents);
res.end();
} catch (fileReadErr) {
if (reqPathStat.isDirectory()) {
// render directory listing
try {
const filepath = path.join(serveDir, reqPath);
const dirlinks = ['..', ...fs.readdirSync(filepath)]
.map((file) => {
const fileStat = fs.lstatSync(path.join(reqPathFSPath, file));
const filename = fileStat.isDirectory() ? `${file}/` : file;
return `<li><a href="${path.join(
reqPath,
filename
)}">${filename}</a></li>`;
})
.join('\n');
console.log(`200 ${req.url} [dir listing] 'text/html'`);
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(`<!DOCTYPE html>
<html>
<head>
<title>Directory listing of ${reqPath}</title>
</head>
<body>
<h1>Directory listing of ${reqPath}</h1>
<ul>${dirlinks}</ul>
</body>
</html>`);
res.end();
return;
} catch (dirlistErr) {
// directory listing failed somehow
return errRes(dirlistErr, 500);
}
} else {
// there was a file or dir but we couldn't read it
return errRes(fileReadErr, 500);
}
}
}
const server = http.createServer(handler);
server.listen(port);
console.log(
`
opening http://127.0.0.1:${port}/ in your browser
press CTRL-C to quit this program
`
);