-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
53 lines (48 loc) · 1.78 KB
/
Copy pathmain.ts
File metadata and controls
53 lines (48 loc) · 1.78 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
import http from 'http';
import querystring from 'querystring';
import path from 'path';
import fs from 'fs';
const hostname = '127.0.0.1';
const port = 3000;
const publicPath = path.join(__dirname, './public');
const wait = (time: number) => new Promise(resolve => {
setTimeout(resolve, time);
});
const makeScript = (jsName: string, needWait: number = 0, url?: string, type?: string) => {
const waitStr = needWait > 0 ? ` wait: ${needWait}` : '';
let script = `console.log('${jsName}${waitStr}');`;
if (url) {
script += `document.write('<script ${type || ''} src="${url}"></script>');`;
}
return script;
}
const server = http.createServer(async (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
const [reqPath, queryStr] = (req.url || '').split('?');
const query = querystring.decode(queryStr || '');
if (reqPath.endsWith('.js')) {
res.setHeader('Content-Type', 'application/x-javascript');
const jsName = reqPath.split('/').pop() || '';
const needWait = (query.wait && +query.wait) || 0;
const moreUrl = Array.isArray(query.url) ? query.url[0] : query.url;
const moreType = Array.isArray(query.type) ? query.type[0] : query.type;
if (needWait > 0) {
await wait(needWait);
}
res.write(makeScript(jsName, needWait, decodeURIComponent(moreUrl || ''), moreType || ''));
} else {
let htmlName = 'index.html';
if (reqPath.endsWith('.html')) {
htmlName = reqPath.split('/').pop() || '';
}
const content = fs.readFileSync(path.join(publicPath, htmlName), 'utf-8');
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.write(content);
}
res.end();
});
server.listen(port, hostname, () => {
const url = `http://${hostname}:${port}/`;
console.log(`Server running at ${url}`);
});