-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (53 loc) · 1.63 KB
/
Copy pathserver.js
File metadata and controls
59 lines (53 loc) · 1.63 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
56
57
58
59
import http from "http";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const server = http.createServer((req, res) => {
// Handle requests for static files (like CSS and JS)
if (req.url.startsWith("/public/")) {
const filePath = path.join(__dirname, req.url);
const ext = path.extname(filePath);
const contentType =
ext === ".css"
? "text/css"
: ext === ".js"
? "application/javascript"
: "text/plain";
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404);
res.end("File not found");
return;
}
res.writeHead(200, { "Content-Type": contentType });
res.end(data);
});
return;
}
// Handle page routes
let filePath;
if (req.url === "/" || req.url === "/index.html") {
filePath = path.join(__dirname, "index.html");
} else if (req.url === "/about") {
filePath = path.join(__dirname, "about.html");
} else if (req.url === "/contact") {
filePath = path.join(__dirname, "contact.html");
} else {
filePath = path.join(__dirname, "404.html");
}
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Internal Server Error");
return;
}
res.writeHead(200, { "Content-Type": "text/html" });
res.end(data);
});
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`🌍 Server running at http://localhost:${PORT}/`);
});