-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (39 loc) · 1.17 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
var http = require('http');
var url = require('url');
var console = require('console');
var Router = require('routes');
var router = new Router();
var host = "127.0.0.1";
var port = "1337";
router.addRoute('/', root);
router.addRoute('/1/:ceiling?', problem1);
http.createServer(function (req, res) {
var path = url.parse(req.url).pathname;
var match = router.match(path);
if (match) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var result = match.fn(match.params);
res.statusCode = 200;
res.end("Result " + result);
} else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.statusCode = 200;
res.end("No route match");
}
}).listen(port, host);
function root(req, res, match) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.statusCode = 200;
res.end("root");
}
function problem1(params) {
var result = 0;
var ceiling = params.ceiling ? params.ceiling : 1000;
for (var i = 0; i < ceiling; i++) {
if (i % 3 == 0 || i % 5 == 0) {
result += i;
}
}
return result;
}
console.log('Server running at http://' + host + ':' + port);