-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
67 lines (64 loc) · 2.34 KB
/
index.ts
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
;
import { createServer, IncomingMessage, Server, ServerResponse } from "http";
import csv from "./code/csv.js";
import humanTime from "./code/humanTime.js";
import log from "./code/log.js";
import query from "./code/query.js";
import test from "./code/test.js";
import vars from "./code/vars.js";
csv("./Zip_Code_Lookup_Table.csv", function (data:data):void {
{
const index:number = process.argv.indexOf("test");
if (index > -1) {
process.argv.splice(index, 1);
vars.test = true;
}
}
const listener = function (request:IncomingMessage, response:ServerResponse):void {
const respond = function (code:number, message:string, type:string):void {
response.writeHead(code, {"content-type": type});
response.write(message);
response.end();
};
if (request.method.toLowerCase() === "get") {
vars.queryTime = process.hrtime.bigint();
respond(200, query(request.url, data), "text/plain");
} else {
const html:string[] = [
"<!DOCTYPE html>",
"<html><head>",
"<meta charset=\"utf-8\"/>",
"<title>Zip Codes</title>",
`<p>This server only accepts GET request method, but received ${request.method}.</p>`,
"</body></html>"
];
respond(403, html.join("\n"), "text/html");
}
},
serverCallback = function ():void {
humanTime(vars.serverTime);
if (vars.test === true) {
test();
}
},
server:Server = createServer(listener);
// discover port option
{
let len:number = process.argv.length;
do {
len = len - 1;
if (isNaN(Number(process.argv[len])) === false) {
vars.port = Number(process.argv[len]);
}
} while (len > 0);
}
if (vars.test === true) {
log.title("Tests");
} else {
log.title("Server");
}
log([`Executing an HTTP server on port: ${vars.text.green + vars.port + vars.text.none}`]);
server.listen({
port: vars.port
}, serverCallback);
});