-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
61 lines (55 loc) · 1.78 KB
/
index.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
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 8080;
const PUBLIC_DIR = path.join(__dirname, 'public');
const server = http.createServer((req, res) => {
// Default to serving index.html if root URL is requested
let filePath = req.url === '/' ? '/index.html' : req.url;
filePath = path.join(PUBLIC_DIR, filePath);
// Determine content type based on file extension
const extname = path.extname(filePath);
let contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'application/javascript';
break;
case '.wasm':
contentType = 'application/wasm';
break;
case '.css':
contentType = 'text/css';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
contentType = 'image/jpg';
break;
}
// Read the file and serve it
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
// If file not found, serve a 404
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 Not Found</h1>', 'utf-8');
} else {
// Server error
res.writeHead(500);
res.end(`Server error: ${err.code}`, 'utf-8');
}
} else {
// Successful response
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
});
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
// Arnim was here