forked from Rochester-CS-Club/CS-CLUB-WEBSITE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.cjs
59 lines (48 loc) · 1.57 KB
/
index.cjs
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
const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');
const server = http.createServer((req, res) => {
const reqUrl = url.parse(req.url, true);
const route = reqUrl.pathname;
if (route === '/') {
fs.readFile(path.join(__dirname, 'index.html'), (err, data) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Server error');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
}
});
}
else if (route === '/attendance') {
fs.readFile(path.join(__dirname, 'attendance.html'), (err, data) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Server error');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
}
});
}
else {
// Try to serve static files
const staticFilePath = path.join(__dirname, route);
fs.readFile(staticFilePath, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Page not found');
} else {
res.writeHead(200);
res.end(data);
}
});
}
});
const port = 3010;
const host = 'localhost';
server.listen(port, host, () => {
console.log(`Server running at http://${host}:${port}/`);
});