-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
55 lines (46 loc) · 1.84 KB
/
app.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
//if server is running on Nicklas' server set this to true,
//if run locally set to false
const online = true;
import * as http from 'http';
import * as fs from 'fs';
let hostname;
online ? hostname = '192.168.1.72' : hostname = 'localhost';
const port = 8000;
import { operator, event } from './Server/classes.mjs';
import { postHandler, getHandler, fileResponse, errorResponse } from './Server/responseHandlers.mjs';
//Create server object with the function requestHandler as input
const server = http.createServer(requestHandler);
//Tells server to listen on ip and port
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
fs.writeFileSync('message.txt', `Server running at http://${hostname}:${port}/`);
});
//Process a request, if it failes respond with error 500
function requestHandler(req, res) {
try {
processReq(req, res);
} catch (err) {
errorResponse(res, 500, "Internal Error" + err);
}
}
//Function to process a request, can fail hence the function requestHandler
function processReq(req, res) {
//Remove the first "/" as we do not use absolute paths
req.url = req.url.substring(1);
//The webpages initial page is simply "/" which was just removed so it's
//now "" but the actual page is "Pages/index.html", so we make a special case
//for "/"
req.url == "" ? req.url = "Pages/index.html" : req;
//Depending on http method used, different handlers handle the request. If an
//unexpected method type appears we attempt to respond with a default file
//response
console.log("Request: " + req.method + " " + req.url);
switch (req.method) {
case 'POST':
return postHandler(req, res);
case 'GET':
return getHandler(req, res);
default:
return fileResponse(req.url, res);
}
}