-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
117 lines (102 loc) · 3.35 KB
/
Copy pathserver.js
File metadata and controls
117 lines (102 loc) · 3.35 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
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
const https = require('https');
const http = require('http');
const fs = require('node:fs');
const path = require('path');
// setup router by using all files from the "lib/" directory
let dir = path.join(__dirname, 'lib');
let files = fs.readdirSync(dir).map(f => path.join(dir, f));
let staticRoutes = {};
let dynamicRoutes = [];
console.log('Loading routes')
for (let i = 0; i < files.length; i++) {
let file = files[i];
// if file is a directory, then add to the queue.
if (fs.lstatSync(file).isDirectory()) {
files = files.concat(fs.readdirSync(file).map(f => path.join(file, f)));
continue;
}
// if not a js file, skip
if (!file.match(/.js$/)) {
continue;
}
let fileExports = require(file);
// check for routes export
if (fileExports === undefined || !fileExports.hasOwnProperty('routes')) {
continue; // file does not export any routes
}
let currentRoutes = fileExports.routes;
if (!Array.isArray(currentRoutes)) {
console.warn('Exported routes is list: ' + file);
continue;
}
// add each route handler to the global map
let requriedProperties = ['method', 'path', 'handler'];
for (let route of currentRoutes) {
// check if the handler has the required properties
let valid = true;
requriedProperties.forEach(p => {
if (!route.hasOwnProperty(p)) {
console.warn(file + ": missing " + p);
valid = false;
}
})
if (!valid) {
continue;
}
// add the route to the map/list
let routePath = route.path
if (routePath instanceof RegExp) {
dynamicRoutes.push(route);
console.log(routePath);
} else if (typeof routePath === 'string') {
if (staticRoutes.hasOwnProperty(routePath)) {
console.warn('Duplicate route ' + routePath + ' in file ' + file);
continue;
}
staticRoutes[routePath] = route;
console.log(routePath);
}
}
}
console.log("Loaded " + (Object.keys(staticRoutes).length + dynamicRoutes.length) + " routes from " + files.length + " files.")
async function handleRequest(req, res) {
let url = req.url;
console.log(url);
// check if matches any static or dynamic routes
let route = staticRoutes[url];
if (route === undefined) {
for (let dh of dynamicRoutes) {
if (dh.path.test(url)) {
route = dh;
}
}
}
if (route === undefined) {
console.warn('404 on route ' + url)
res.writeHead(404, {'Content-type': 'text/plain'});
res.end('Resource not found for route ' + url + '.')
return;
}
// check method matches the route
let method = req.method;
if (method != route.method) {
console.warn('405 on route ' + url);
res.writeHead(405, {'Content-type': 'text/plain'});
res.end('Method ' + route.method + " required for route " + url + '.')
return;
}
// run the route's handler
await route.handler(req, res);
}
let sslPath = path.join(__dirname, 'ssl')
const options = {
key: fs.readFileSync(path.join(sslPath, 'server.key')),
cert: fs.readFileSync(path.join(sslPath, 'server.crt'))
}
const server = https.createServer(options, handleRequest)
if (require.main === module) {
console.log('Listening on port 8080');
server.listen(8080);
// httpServer.listen(8090);
}
module.exports = server;