main.js
8.27 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var http = require('http');
var fs = require('fs');
var url = require('url');
var qs = require('querystring');
var template = require('./lib/template.js');
/*
const style={
backgrondColor = 'blue',
border:'1px solid border'
}*/
const { count } = require('console');
var app = http.createServer(function(request,response){
var _url = request.url;
var queryData = url.parse(_url, true).query;
var pathname = url.parse(_url, true).pathname;
if(pathname === '/'){
if(queryData.id === undefined){
var title = '소개';
var description = '개개인의 신체 정보에 따라 일일 권장 섭취량을 알려주고, 좋아하는 음식을 검색하면 영양정보를 알려주는 사이트 입니다.';
var html = template.html(title,`
<br><br><br><br>
<style>
@import url('https://fonts.googleapis.com/css2?family=Jua&family=Noto+Sans+KR&display=swap');
</style>
<h2 style="font-family: 'Jua', sans-serif;"><font color="red"><text align:center>${title}</font></h2>
<p style="font-family: 'Jua', sans-serif;">${description}</p>` );
response.writeHead(200);
response.end(html);
}
}
else if(pathname === '/countKcal'){
var title = '칼로리 계산';
var description = '성별, 나이, 키, 몸무게, 활동량을 입력하시면 일일 권장 섭취량(kcal)를 알려드립니다!';
var html = template.html(title,`
<h2>${title}</h2>
<p>${description}</p>
<form action="/countKcal_process" method="post" >
성별 :<br />
<select name="gender">
<option value="man">남자</option>
<option value="woman">여자</option></select
><br />
나이(세) : <br />
<input type="text" name="age" /><br />
키(cm) : <br />
<input type="text" name="height" /><br />
몸무게(kg) : <br /><input type="text" name="weight" /><br />
활동량 :<br />
<select name="activity">
<option value="low">앉아 있거나 가벼운 활동(운동을 거의 또는 전혀하지 않음)</option>
<option value="mid">활동적 또는 보통 활동(건설노동자 또는 매일 1시간씩 달림)</option>
<option value="high">많은 활동(농업노동자 또는 매일 2시간동안 수영을 함)</option>
</select
><br />
<input type="submit" value="계산하기" />
</form>` );
response.writeHead(200);
response.end(html);
}
else if(pathname === '/countKcal_process'){
var body = '';
request.on('data', function(data){
body = body + data;
});
request.on('end', function(){
var post = qs.parse(body);
var title = '칼로리 계산';
var description = '성별, 키, 몸무게, 활동량을 입력하시면 표준체중, 현재 체중을 유지하는데 필요한 열량, 다이어트 열량을 알려드립니다!';
var gender = post.gender;
var age = post.age;
var height = post.height;
var weight = post.weight;
var BMR; //기초대사량(bmr) Harris-BenedictEquation(B.E.E) 방법
var BMI = (weight/(height*height)*10000).toFixed(1); //비만도
var BMI_msg = '';
if(BMI >= 30){
BMI_msg += '고도비만';
}
else if(BMI >=25 && BMI <30){
BMI_msg += '비만';
}
else if(BMI >=23 && BMI <25){
BMI_msg += '과체중';
}
else if(BMI >=18.5 && BMI <23){
BMI_msg += '정상';
}
else if(BMI >= 0 && BMI < 18.5){
BMI_msg += '저체중';
}
else {
BMI_msg += "Error";
}
var gender_default = '';
if(gender == "man"){
gender_default =
`<option value="man" selected>남자</option>
<option value="woman">여자</option>`;
BMR = (66.5 + 13.75*weight + 5.003*height - 6.755*age).toFixed(1);
}
else {
gender_default =
`<option value="man" >남자</option>
<option value="woman" selected>여자</option>`;
BMR = (655. + 9.563*weight + 1.85*height - 4.676*age).toFixed(1);
}
var std_weight = (height*height/10000*((gender =="man") ? 22 : 21)).toFixed(1); //남자면 22, 여자면 21을 곱함.
var activity = post.activity;
var activity_idx;
var TDEE; //Total Daily Energy Expenditure 현재 체중을 유지하기 위한 열량
var activity_default = '';
var weight_gap = (weight - std_weight).toFixed(1);
var weightMsg = "";
if(weight_gap > 0){
weightMsg += `표준 체중보다 ${weight_gap}kg 많습니다.`
}
else if(weight_gap < 0){
weightMsg += `표준 체중보다 ${-weight_gap}kg 적습니다.`
}
else {
weightMsg += '표준 체중과 몸무게가 같습니다.'
}
switch(activity){
case "low":
activity_idx = 1.53;
TDEE = (BMR*activity_idx).toFixed(1);
activity_default = `
<option value="low" selected>앉아 있거나 가벼운 활동(운동을 거의 또는 전혀하지 않음)</option>
<option value="mid">활동적 또는 보통 활동(건설노동자 또는 매일 1시간씩 달림)</option>
<option value="high">활발히 활동(농업노동자 또는 매일 2시간동안 수영을 함)</option>
</select>
`
break;
case "mid":
activity_idx = 1.76;
TDEE = (BMR*activity_idx).toFixed(1);
activity_default = `
<option value="low" >앉아 있거나 가벼운 활동(운동을 거의 또는 전혀하지 않음)</option>
<option value="mid" selected>활동적 또는 보통 활동(건설노동자 또는 매일 1시간씩 달림)</option>
<option value="high">많은 활동(농업노동자 또는 매일 2시간동안 수영을 함)</option>
</select>
`
break;
default:
activity_idx = 2.25;
TDEE = (BMR*activity_idx).toFixed(1);
activity_default = `
<option value="low" >앉아 있거나 가벼운 활동(운동을 거의 또는 전혀하지 않음)</option>
<option value="mid" >활동적 또는 보통 활동(건설노동자 또는 매일 1시간씩 달림)</option>
<option value="high" selected>많은 활동(농업노동자 또는 매일 2시간동안 수영을 함)</option>
</select>
`
}
var html = template.html(title,`
<h2>${title}</h2>
<p>${description}</p>
<form action="/countKcal_process" >
성별 :<br />
<select name="gender">
${gender_default}
</select>
<br />
키(cm) : <br /><input type="text" name="height" placeholder="${height}" /><br />
몸무게(kg) : <br /><input type="text" name="weight" placeholder="${weight}"/><br />
활동량 :<br />
<select name="activity">
${activity_default}
<br />
<input type="submit" value="계산하기" />
<br>
표준체중 : ${std_weight}kg <br>
${weightMsg}<br>
BMI 지수 : ${BMI} / 당신은 ${BMI_msg} 입니다. <br>
기초 대사량 : ${BMR}kcal <br>
현재 체중 유지를 위한 열량 : ${TDEE}kcal <br>
</form>
` );
response.writeHead(200);
response.end(html);
});
}
else{
response.writeHead(404);
response.end("Not found");
}
});
app.listen(23000);