-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (90 loc) · 3.28 KB
/
index.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
/**
* Primary file for the API
*/
// Dependencies
import {createServer as createHttpServer} from "http";
import {createServer as createHttpsServer} from "https";
import {StringDecoder} from "string_decoder";
import {readFileSync} from "fs";
import {environmentToExport as config} from "./config.js";
import {handlers} from "./lib/handlers.js";
import {helpers} from "./lib/helpers.js";
// All the server logic for doth the http and https server
const unifiedServer = function (req, res) {
// Get url and parse it.
const parseURL = new URL(req.url, "http://localhost:3000");
// Get path
const path = parseURL.pathname;
const trimPath = path.replace(/^\/+|\/+$/g, '');
// Get the query string as an object
const queryStringObject = parseURL.searchParams;
// Get the HTTP Method
const method = req.method.toLowerCase();
// Get the headers as an object
const headers = req.headers;
// Get the payload, if any
const decoder = new StringDecoder("utf-8");
let buffer = "";
req.on("data", function (data) {
buffer += decoder.write(data);
});
req.on("end", function () {
buffer += decoder.end();
// Choose the handler this request should go to.
const chosenHandler = typeof (router[trimPath]) !== "undefined" ? router[trimPath] : handlers.notFound;
// Construct the data object to send to the handler
const data = {
trimPath,
queryStringObject,
method,
headers,
payload: helpers.parseJsonToObject(buffer)
};
// Route the request to the handler specify in the router
chosenHandler(data, function (statusCode, payload) {
// Use the status code called back by the handler, or default to 200
statusCode = typeof (statusCode) === "number" ? statusCode : 200;
// Use the payload called back by the handler, or default to
payload = typeof (payload) === "object" ? payload : {};
// Convert payload to a string
const payloadString = JSON.stringify(payload);
// send to response
res.setHeader("Content-Type", "application/json");
res.writeHead(statusCode);
res.end(payloadString);
// log the request path
console.log("Returning the response:", statusCode, payloadString);
});
// log the request path.
console.log(`
Request received on path: ${trimPath}
Request was w/ this method: ${method}
Query string parameters: ${queryStringObject}
Request received w/ headers: ${JSON.stringify(headers)}
Request received w/ payload: ${buffer}
`);
});
};
// Instantiate the HTTP server
const httpServer = createHttpServer(unifiedServer);
// Instantiate the HTTPS server
const httpsServerOptions = {
"key": readFileSync("./https/key.pem"),
"cert": readFileSync("./https/cert.pem"),
};
const httpsServer = createHttpsServer(httpsServerOptions, unifiedServer);
console.clear();
// Start the HTTP server
httpServer.listen(config.httpPort, function () {
console.log(`The HTTP server is listening on port ${config.httpPort} in ${config.envName} now.`);
});
// Start the HTTPS server
httpsServer.listen(config.httpsPort, function () {
console.log(`The HTTPS server is listening on port ${config.httpsPort} in ${config.envName} now.`);
});
const router = {
"ping": handlers.ping,
"users": handlers.users,
"tokens": handlers.tokens,
"checks": handlers.checks,
};