-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathstatic.js
43 lines (38 loc) · 1.27 KB
/
static.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
'use strict';
const http = require('node:http');
const path = require('node:path');
const fs = require('node:fs');
const MIME_TYPES = {
html: 'text/html; charset=UTF-8',
json: 'application/json; charset=UTF-8',
js: 'application/javascript; charset=UTF-8',
css: 'text/css',
png: 'image/png',
ico: 'image/x-icon',
svg: 'image/svg+xml',
};
const HEADERS = {
'X-XSS-Protection': '1; mode=block',
'X-Content-Type-Options': 'nosniff',
'Strict-Transport-Security': 'max-age=31536000; includeSubdomains; preload',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
};
module.exports = (root, port, console) => {
http.createServer(async (req, res) => {
const url = req.url === '/' ? '/index.html' : req.url;
const filePath = path.join(root, url);
try {
const data = await fs.promises.readFile(filePath);
const fileExt = path.extname(filePath).substring(1);
const mimeType = MIME_TYPES[fileExt] || MIME_TYPES.html;
res.writeHead(200, { ...HEADERS, 'Content-Type': mimeType });
res.end(data);
} catch (err) {
res.statusCode = 404;
res.end('"File is not found"');
}
}).listen(port);
console.log(`Static on port ${port}`);
};