Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (47 loc) · 1.46 KB
/
Copy pathserver.js
File metadata and controls
56 lines (47 loc) · 1.46 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 8000;
const mimeTypes = {
'.html': 'text/html',
'.yaml': 'text/yaml',
'.yml': 'text/yaml',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json'
};
const server = http.createServer((req, res) => {
// Add CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
let filePath = '.' + req.url;
if (filePath === './') {
filePath = './workflow-status.html';
}
const extname = String(path.extname(filePath)).toLowerCase();
const contentType = mimeTypes[extname] || 'application/octet-stream';
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === 'ENOENT') {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 - File Not Found</h1>', 'utf-8');
} else {
res.writeHead(500);
res.end(`Server Error: ${error.code}`, 'utf-8');
}
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
});
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
console.log(`Open http://localhost:${PORT}/workflow-status.html in your browser`);
});