-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.js
More file actions
67 lines (59 loc) · 2.18 KB
/
server.js
File metadata and controls
67 lines (59 loc) · 2.18 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
59
60
61
62
63
64
65
66
67
'use strict';
const fs = require('node:fs');
const http = require('node:http');
const path = require('node:path');
const { Readable } = require('node:stream');
const PORT = 8000;
const MIME_TYPES = {
default: 'application/octet-stream',
html: 'text/html; charset=UTF-8',
js: 'application/javascript; charset=UTF-8',
json: 'application/json',
css: 'text/css',
png: 'image/png',
jpg: 'image/jpg',
gif: 'image/gif',
ico: 'image/x-icon',
svg: 'image/svg+xml',
};
const STATIC_PATH = path.join(process.cwd(), './static');
const folderIndex = (folder) => new Readable({
async read() {
const files = [];
const folders = [];
const rel = folder.substring(STATIC_PATH.length);
const items = await fs.promises.readdir(folder, { withFileTypes: true });
for (const item of items) {
if (item.isDirectory()) folders.push(item.name + '/');
else files.push(item.name);
}
const list = folders.concat(files)
.map((item) => `<li><a href="${rel}/${item}">${item}</a></li>`)
.join('\n');
this.push(`<h2>Directory index:</h2><ul>${list}</ul>`);
this.push(null);
}
});
const prepareFile = async (url) => {
const name = url === '/' ? '/index.html' : url;
const filePath = path.join(STATIC_PATH, name);
const pathTraversal = !filePath.startsWith(STATIC_PATH);
const stat = await fs.promises.lstat(filePath).catch(() => false);
const exists = !!stat;
const isDirectory = stat && stat.isDirectory();
const found = !pathTraversal && exists;
const streamPath = found ? filePath : STATIC_PATH + '/404.html';
const ext = path.extname(streamPath).substring(1).toLowerCase();
const factory = isDirectory ? folderIndex : fs.createReadStream;
const stream = factory(streamPath);
return { found, ext: isDirectory ? 'html' : ext, stream };
};
http.createServer(async (req, res) => {
const file = await prepareFile(req.url);
const statusCode = file.found ? 200 : 404;
const mimeType = MIME_TYPES[file.ext] || MIME_TYPES.default;
res.writeHead(statusCode, { 'Content-Type': mimeType });
file.stream.pipe(res);
console.log(`${req.method} ${req.url} ${statusCode}`);
}).listen(PORT);
console.log(`Server running at http://127.0.0.1:${PORT}/`);