-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
127 lines (111 loc) · 3.15 KB
/
server.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
/**
* Takes a Roman numeral string and returns it's decimal value
* @param string romanString The Roman Numeral input
* @return integer the decimal value or -1 in case of a malformatted string
*/
function romanToDecimal(romanString) {
var output = 0, i;
romanString = romanString.toUpperCase();
/* Check for a few special cases - substractions. */
// IV = 4
i = romanString.match(/IV/g);
output += ( i ? i.length : 0 ) * 4;
romanString = romanString.replace(/IV/g, '');
// IX = 9
i = romanString.match(/IX/g);
output += ( i ? i.length : 0 ) * 9;
romanString = romanString.replace(/IX/g, '');
// XL = 40
i = romanString.match(/XL/g);
output += ( i ? i.length : 0 ) * 40;
romanString = romanString.replace(/XL/g, '');
// XC = 90
i = romanString.match(/XC/g);
output += ( i ? i.length : 0 ) * 90;
romanString = romanString.replace(/XC/g, '');
// CD = 400
i = romanString.match(/CD/g);
output += ( i ? i.length : 0 ) * 400;
romanString = romanString.replace(/CD/g, '');
// CM = 900
i = romanString.match(/CM/g);
output += ( i ? i.length : 0 ) * 900;
romanString = romanString.replace(/CM/g, '');
/* Calculate standard cases */
for (i = 0; i < romanString.length; i++) {
switch(romanString[i]) {
case 'M':
output += 1000;
break;
case 'D':
output += 500;
break;
case 'C':
output += 100;
break;
case 'L':
output += 50;
break;
case 'X':
output += 10;
break;
case 'V':
output += 5;
break;
case 'I':
output += 1;
break;
default:
return -1;
break;
}
}
return output;
}
var http = require("http"), url = require("url"), fs = require('fs');
http.createServer(function(request, response) {
var called_url = url.parse(request.url, true);
if(called_url.pathname === '/' || called_url.pathname === '/index.html') {
fs.readFile('./index.html', function(err, contents) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write("Error loading index template file.");
response.end();
return;
}
response.writeHead(200, {"Content-Type": "text/html"});
response.write(contents);
response.end();
});
} else if(called_url.pathname === '/calculate') {
if(typeof(called_url.query.number) !== 'undefined') {
var retorno = romanToDecimal(called_url.query.number);
if(retorno >= 0) {
response.writeHead(200, {"Content-Type": "text/json"});
response.write('{"status": "OK", "result": ' + retorno + '}');
response.end();
return;
}
}
response.writeHead(500, {"Content-Type": "text/json"});
response.write('{"status": "error"}');
response.end();
} else if (called_url.pathname === '/bootstrap.min.css' || called_url.pathname === '/bootstrap.min.js') {
fs.readFile('.' + called_url.pathname, function(err, contents) {
if(err) {
response.writeHead(500, {"Content-Type": "text/css"});
response.write("");
response.end();
return;
}
response.writeHead(200, {"Content-Type": "text/css"});
response.write(contents);
response.end();
});
}
else {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("Not found.");
response.end();
}
}).listen(8080);