-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
43 lines (36 loc) · 1.19 KB
/
server.js
File metadata and controls
43 lines (36 loc) · 1.19 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
const http = require("http");
const fs = require("fs");
const path = require("path");
const PORT = process.env.PORT || 3000;
const server = http.createServer((req, res) => {
// Serve index.html
if (req.url === "/" || req.url === "/index.html") {
fs.readFile("./index.html", (err, data) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/plain" });
return res.end("Error loading page");
}
res.writeHead(200, { "Content-Type": "text/html" });
res.end(data);
});
}
// Serve styles.css <-- updated filename
else if (req.url === "/styles.css") {
fs.readFile("./styles.css", (err, data) => {
if (err) {
res.writeHead(404, { "Content-Type": "text/plain" });
return res.end("CSS file not found");
}
res.writeHead(200, { "Content-Type": "text/css" });
res.end(data);
});
}
// All other routes
else {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("404 Not Found");
}
});
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});