-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
118 lines (108 loc) · 3.26 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import http from "http";
import fs from "fs";
import path from "path";
import url from "url";
import { glob } from "glob";
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const server = http.createServer((req, res) => {
// Splitting the URL by slashes
const urlParts = req.url.split("/");
// Remove the first item if it's blank
const urlTrimmed = urlParts.filter((n) => n);
// Remove base URL for dev processing
const urlBaseTrimmed =
urlTrimmed[0] === "tailwind-infinix-website"
? urlTrimmed.slice(1)
: urlTrimmed;
// Output the parts
const urlPartsConcat = "/" + urlBaseTrimmed.join("/");
console.log("Trimmed: ", urlTrimmed);
console.log("Concat: ", urlPartsConcat);
if (urlTrimmed[0] === "tailwind-infinix-website")
if (isStaticFile(urlPartsConcat)) {
console.log("media");
// Check if the requested URL matches a media file
const mediaFile = getMediaFile(decodeURI(urlPartsConcat));
if (mediaFile) {
serveMediaFile(res, mediaFile);
} else {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("404 Not Found");
}
} else {
console.log("html");
serveHTMLFile(res, "index.html");
// serveHTMLFile(res, "404.html");
// switch (req.url) {
// case "/":
// serveHTMLFile(res, "index.html");
// break;
// default:
// res.writeHead(302, { Location: "/" });
// res.end();
// break;
// }
}
else {
console.log("first part of url is NOT VALID");
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("404 Not Found");
}
});
// Function to check if the requested URL corresponds to a static file
function isStaticFile(url) {
return url.match("(/static/.+\\..+|/.+\\.(ico))");
}
// Function to match route using glob pattern
function getRoute(url) {
const routes = glob.sync("*.html");
const filename = path.basename(url);
console.log("path: ", filename);
const matchedRoute = routes.find((route) => {
console.log("route", path.basename(route));
return path.basename(route) === filename;
});
return matchedRoute ? matchedRoute : null;
}
// Function to serve HTML files
function serveHTMLFile(res, filename) {
console.log(filename);
fs.readFile(filename, (err, data) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Internal Server Error");
} else {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(data);
}
});
}
// Function to match media files
function getMediaFile(url) {
const filePath = path.join(__dirname, url);
console.log("filepath ", fs.existsSync(filePath));
if (fs.existsSync(filePath)) {
return filePath;
}
return null;
}
// Function to serve media files
function serveMediaFile(res, filename) {
fs.readFile(filename, (err, data) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Internal Server Error");
} else {
res.writeHead(200);
res.end(data);
}
});
}
// Starting the server
const PORT = 3000;
server.listen(PORT, () => {
console.log("env: ", process.env.DEVELOPMENT);
console.log(`Server is running on port ${PORT}`);
});
export {};