-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
39 lines (36 loc) · 1.15 KB
/
server.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
import fs from 'node:fs';
import http from 'node:http';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const hostname = '127.0.0.1';
const PORT = 5500;
const server = http.createServer((req, res) => {
const filePath = path.join(__dirname, req.url === '/' ? 'index.html' : req.url);
const extname = String(path.extname(filePath)).toLowerCase();
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
};
const contentType = mimeTypes[extname] || 'application/octet-stream';
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
}
else {
res.writeHead(500);
res.end(`Server Error: ${err.code}`);
}
}
else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf8');
}
});
});
server.listen(PORT, hostname, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});