-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.js
68 lines (60 loc) · 1.89 KB
/
serve.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
const fs = require('fs');
const path = require('path');
const root = __dirname;
const mimes = {
htm: 'text/html',
html: 'text/html',
txt: 'text/plain',
md: 'text/markdown',
css: 'text/css',
js: 'application/javascript',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
png: 'image/png',
gif: 'image/gif',
svg: 'image/svg+xml',
};
const server = require('http').createServer((req, res) => {
if (req.method !== 'GET') {
res.statusCode = 501;
res.setHeader('Content-Type', 'text/plain');
return res.end('Method not implemented');
}
// Fetch Request Path
let request_path = req.url.toString().split('?')[0].trimEnd('/');
if (request_path === '' || request_path === '/') {
request_path = '/index.html';
}
if (request_path.startsWith('/bs-lightbox')) {
request_path = request_path.slice('/bs-lightbox'.length);
}
// Print Request
console.log(`[${req.method}] ${request_path}`);
// LocalPath
let local_path = path.join(root, 'docs', request_path);
if (local_path.indexOf(root) !== 0) {
req.statusCode = 403;
res.setHeader('Content-Type', 'text/plain');
return res.end('Forbidden');
}
// Check If file Exists
if (!fs.existsSync(local_path)) {
req.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
return res.end('File not Found');
}
// Serve File
let type = mimes[path.extname(local_path).slice(1)] || 'text/plain';
let stream = fs.createReadStream(local_path);
stream.on('open', () => {
res.statusCode = 200;
res.setHeader('Content-Type', type);
stream.pipe(res);
});
stream.on('error', () => {
res.statuscode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('Unknown File Error');
});
});
server.listen(3000);