-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfortune.js
More file actions
147 lines (132 loc) · 3.95 KB
/
fortune.js
File metadata and controls
147 lines (132 loc) · 3.95 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var PORT = 8080;
var HOSTNAME = '127.0.0.1';
// load necessary modules
var http = require('http');
var querystring = require('querystring');
var crypto = require('crypto')
//var htmlHeader = '<!DOCTYPE HTML>
var htmlHeader = (function() {/*
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>fortune telling</title>
<style>
.content {
width: 480px;
text-align: center;
border; 4px solid lightblue;
padding; 4px;
margin; 16px auto;
}
.main-form div {margin-bottom; 4px;}
.result {
display; black;
font-size; 200%;
color; red;
margin; 4px auto;
border; 1px solid;
width; 4em;
}
</style>
</html>
<body>
<div class="content">
<h1>fortune telling</h1>
*/}).toString().match(/\/\*([^]*)\*\//)[1];
var htmlMainForm = (function() {/*
<div class="main-form">
<form method="post" action="/">
<div>
<label>名前:<input type="text" name="name" size="20"></label>
</div>
<div>
生年月日:
<label><input type="text" name="year" size="5">年</label>
<label><input type="text" name="month" size="3">月</label>
<label><input type="text" name="day" size="3">日</label>
</div>
<div>
性別:
<label><input type="radio" name="sex" value="male">男</label>
<label><input type="radio" name="sex" value="female">女</label>
</div>
<input type="submit" value="占う">
</form>
</div>
*/}).toString().match(/\/\*([^]*)\*\//)[1];
var htmlFooter = '</div></body></html>';
// 「<」や「>」、「&」といった文字列をエンティティに変換する
function escapeHtmlSpecialChar(html) {
if (html === undefined) {
return ;
} else {
return require('validator').escape(html);
}
};
//var server = http.createServer(onRequest);
//function onRequest(req, res) {
http.createServer(function (req, res) {
if (req.url !== '/') {
res.writeHead(404, {'Content-type': 'text/plain'});
res.end('Error 404: Not Found.');
return ;
}
// if the request is POST, tell the fortune from data sent.
if (req.method === 'POST') {
// get data sent
req.data = '';
req.on('data', function (chunk) {
req.data += chunk;
});
req.on('end', sendResponse);
return ;
// if the request is not POST, send mainform.
} else {
res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'});
res.write(htmlHeader);
res.write(htmlMainForm);
res.write(htmlFooter);
res.end();
return ;
}
function sendResponse() {
var query = querystring.parse(req.data);
// compute a MD5 hash from a concatenation of the posted data.
var seed = query.name + query.year + query.month + query.day + query.sex;
var hash = crypto.createHash('md5');
hash.update(seed);
var hashValue = hash.digest('hex');
var fortuneKey = Number('0x' + hashValue.slice(0,2));
var result = '';
if (fortuneKey < 10) {
result = '大凶';
} else if (fortuneKey < 50) {
result = '凶';
} else if (fortuneKey < 100) {
result = '末吉';
} else if (fortuneKey < 150) {
result = '吉';
} else if (fortuneKey < 245) {
result = '中吉';
} else {
result = '大吉';
}
var resultStr = '<div><p>'
+ escapeHtmlSpecialChar(query.year) + '年'
+ escapeHtmlSpecialChar(query.month) + '月'
+ escapeHtmlSpecialChar(query.day) + '日生まれの'
+ escapeHtmlSpecialChar(query.name) + 'さん('
+ ((query.sex == 'male') ? '男性' : '女性')
+ ')の運勢は…'
+ '<span class="result">'
+ result + '</span>です。</p></div>'
+ '<a href="/">トップに戻る</a>';
res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'});
res.write(htmlHeader);
res.write(resultStr);
res.write(htmlFooter);
res.end();
}
}).listen(PORT);
console.log('Server running at http://' + HOSTNAME + ':' + PORT + '/');