-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
84 lines (76 loc) · 2.79 KB
/
server.ts
File metadata and controls
84 lines (76 loc) · 2.79 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
'use strict';
import http = require('http');
import fs = require('fs');
import url = require('url');
import {Body} from "node-fetch";
const fetch = require('node-fetch');
const port = 3000;
const contentTypes = new Map();
contentTypes.set('html', 'text/html');
contentTypes.set('htm', 'text/html');
contentTypes.set('js', 'text/javascript');
contentTypes.set('css', 'text/css');
contentTypes.set('json', 'application/json');
contentTypes.set('png', 'image/png');
contentTypes.set('jpg', 'image/jpg');
http.createServer(function (req: http.IncomingMessage, res: http.ServerResponse) {
const q: url.Url = url.parse(req.url, true);
if (q.pathname.substr(0, 5).toLowerCase() != "/api/") {
const fileName: string = (q.pathname === '/' ? 'index.html' : q.pathname.substr(1));
const fileExt: string = fileName.split('.').pop();
const contentType: string = contentTypes.get(fileExt);
fs.readFile("public/" + fileName, function (err: Error | null, data: string | Buffer): void {
if (err || !contentType) {
res.writeHead(404, {'Content-Type': 'text/plain'});
} else {
res.writeHead(200, {'Content-Type': contentType});
res.write(data);
}
res.end();
});
} else {
const callAddress: string = q.path.substr(5).toLowerCase();
cache.fetch(callAddress)
.then((b: Body) => b.text())
.then((body: string) => {
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(body);
res.end();
});
}
}).listen(port, function () {
console.log('Client is available at http://localhost:' + port);
});
//cache actions
const cache = {
timeOut: 3600000,
cacheMem: {},
hash: (str): string => {
let hash = '0';
let i: number;
let chr: number;
if (str.length === 0) return hash;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = '0' + (chr % 8) + hash + chr;
hash = (parseInt(hash, 36) % 60446175).toString(36);
}
return hash;
},
fetch: (call): Promise<Body> => {
return new Promise((resolve, reject) => {
let hash: string = cache.hash(call);
if (cache.cacheMem.hasOwnProperty(hash) && (Date.now() - cache.timeOut < cache.cacheMem[hash].time)) {
resolve(cache.cacheMem[hash].value.clone());
} else {
resolve(
fetch(call)
.then((response): string => {
cache.cacheMem[hash] = {time: Date.now(), value: response};
return (response.clone());
})
);
}
});
}
};