-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.js
More file actions
58 lines (49 loc) · 1.5 KB
/
Copy pathserve.js
File metadata and controls
58 lines (49 loc) · 1.5 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const root = __dirname;
const port = Number(process.env.PORT || 3000);
const host = '127.0.0.1';
const types = {
'.html': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.md': 'text/markdown; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon'
};
function send(res, status, body, type = 'text/plain; charset=utf-8') {
res.writeHead(status, { 'Content-Type': type });
res.end(body);
}
const server = http.createServer((req, res) => {
let urlPath = decodeURIComponent(req.url.split('?')[0]);
if (urlPath === '/' || urlPath === '') {
urlPath = '/index.html';
}
let filePath = path.normalize(path.join(root, urlPath));
if (!filePath.startsWith(root)) {
send(res, 403, 'Forbidden');
return;
}
fs.stat(filePath, (statErr, stat) => {
if (!statErr && stat.isDirectory()) {
filePath = path.join(filePath, 'README.md');
}
fs.readFile(filePath, (readErr, data) => {
if (readErr) {
send(res, 404, 'Not found');
return;
}
const type = types[path.extname(filePath).toLowerCase()] || 'application/octet-stream';
send(res, 200, data, type);
});
});
});
server.listen(port, host, () => {
console.log(`Docsify site is running at http://${host}:${port}`);
});