-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (41 loc) · 1.1 KB
/
server.js
File metadata and controls
48 lines (41 loc) · 1.1 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
const http = require("http");
const fs = require("fs");
const url = require("url");
const path = require("path");
const server = http.createServer((req, res) => {
const requestUrl = url.parse(req.url, true);
const isSPARoute =
requestUrl.pathname === "/" ||
requestUrl.pathname === "/active" ||
requestUrl.pathname === "/completed";
let filePath = path.join(
__dirname,
isSPARoute ? "index.html" : requestUrl.pathname
);
let contentType;
switch (path.extname(filePath)) {
case ".js":
contentType = "application/javascript";
break;
case ".css":
contentType = "text/css";
break;
case ".html":
contentType = "text/html";
break;
default:
contentType = "text/plain";
}
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404);
return res.end("Error loading " + requestUrl.pathname);
}
res.writeHead(200, {
"Content-Type": contentType,
});
res.end(data);
});
});
const port = process.env.PORT || 3000;
server.listen(port, () => console.log(`Server listening on port ${port}`));