-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (104 loc) · 3.29 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Primary file for API
*
*/
// Dependencies
const http = require("http");
const https = require("https");
const url = require("url");
const StringDecoder = require("string_decoder").StringDecoder;
const config = require("./config");
const fs = require("fs");
// Instantiate the HTTP server
const httpServer = http.createServer(function(req, res) {
unifiedServer(req, res);
});
// Start the HTTP server
httpServer.listen(config.httpPort, function() {
console.log("The server is listening on port " + config.httpPort);
});
// Instantiate the HTTPS server
const httpsServerOptions = {
key: fs.readFileSync("./https/key.pem"),
cert: fs.readFileSync("./https/cert.pem")
};
const httpsServer = http.createServer(httpsServerOptions, function(req, res) {
unifiedServer(req, res);
});
// Start the HTTPS server
httpsServer.listen(config.httpsPort, function() {
console.log("The server is listening on port " + config.httpsPort);
});
// All the server logic for both the http and https server
const unifiedServer = function(req, res) {
// Parse the url
const parseUrl = url.parse(req.url, true);
// Get the path
const path = parseUrl.pathname;
const trimmedPath = path.replace(/^\/+|\/+$/g, "");
// Get the query string as an object
const queryStringObject = parseUrl.query;
// 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();
// Check the router for a matching path for a handler. If one is not found, use the notFound handler instead.
const chosenHandler =
typeof router[trimmedPath] !== "undefined"
? router[trimmedPath]
: handlers.notFound;
// Construct the data object to send to the handler
const data = {
trimmedPath: trimmedPath,
queryStringObject: queryStringObject,
method: method,
headers: headers,
payload: buffer
};
// Route the request to the handler specified in the router
chosenHandler(data, function(statusCode, payload) {
// Use the payload returned from the handler, or set the default payload to an empty object
payload = typeof payload == "object" ? payload : {};
// Convert the payload to a string
const payloadString = JSON.stringify(payload);
// Return the response
res.setHeader("Content-Type", "application/json");
res.writeHead(statusCode);
res.end(payloadString);
console.log(trimmedPath, statusCode);
});
});
};
// Define all the handlers
const handlers = {};
// Ping handler
handlers.ping = function(data, callback) {
callback(200);
};
// Hello handler
handlers.hello = function(data, callback) {
console.log(data);
callback(200, {
message: `Hello ${
data.queryStringObject.name !== undefined ? data.queryStringObject.name : ""
} 😃`,
language: data.queryStringObject.lang !== undefined ? data.queryStringObject.lang : ""
});
};
// Not-Found handler
handlers.notFound = function(data, callback) {
callback(404);
};
// Define the request router
const router = {
ping: handlers.ping,
hello: handlers.hello
};