-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoverage.js
More file actions
44 lines (39 loc) · 1.07 KB
/
coverage.js
File metadata and controls
44 lines (39 loc) · 1.07 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
import http from 'http';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PORT = 8000;
const COVERAGE_DIR = path.join(__dirname, 'coverage', 'lcov-report');
const server = http.createServer((req, res) => {
const filePath = path.join(
COVERAGE_DIR,
req.url === '/' ? 'index.html' : req.url,
);
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
} else {
const contentType = getContentType(filePath);
res.writeHead(200, { 'Content-Type': contentType });
res.end(data);
}
});
});
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
const getContentType = (filePath) => {
const extname = path.extname(filePath);
switch (extname) {
case '.html':
return 'text/html';
case '.css':
return 'text/css';
case '.js':
return 'text/javascript';
default:
return 'text/plain';
}
};