-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfibserver.js
41 lines (37 loc) · 882 Bytes
/
fibserver.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
var http = require('http');
const fib = (n) => {
let result = [];
let a = 0n;
let b = 1n;
for (let i = 0; i < n; ++i) {
result.push(a);
[a, b] = [b, a + b];
}
return result;
};
BigInt.prototype.toJSON = function () { return this.toString(); };
const server = http.createServer(
function (req, res) {
const params = req.url.match(
new RegExp("/fibonacci/(\\d+)"));
if (params) {
const n = parseInt(params[1]);
const result = fib(n);
res.writeHead(200,
{
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
});
res.write(JSON.stringify({
'n': n,
'seq': result,
}));
res.end();
}
else {
res.end('Invalid request');
}
});
const PORT = 31337;
server.listen(PORT);
console.log(`Web server listening on port ${PORT} ...`)