search.js
4.01 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
var template = require('./template.js');
var qs = require('querystring');
var db = require('./database.js');
exports.foodInfo = function (request, response) {
var title = '식품 영양정보';
var description = '입력한 음식의 영양 정보(칼로리, 단백질, 지방, 탄수화물, 나트륨)를 알려드립니다!';
var html = template.html(title, `
<div style= "font-size:25px; background-color:#ffffff">
<h2><font color="black">${title}<div style= "font-size:10px;"><br><div style= "font-size:20px;">${description}</font></div></h2>
<form action="/foodInfo_search" method="post">
<input type = 'text' name = 'search' placeholder = '검색어 입력' maxlength = 255 value = "" autocomplete = "off" style="width:300px;height:20px;font-size:20px;">
<button type = "submit" name = "click" style="font-size:20px;">검색</button>
</form>
</div>
`);
response.writeHead(200);
response.end(html);
}
function foodTable(foodInfo, list, num) {
var tag = '';
tag += `<table style='width:100%;'`;
tag += `
<tr>
<td>이름</td>
<td>종류</td>
<td>지역/제조사</td>
<td>1회 제공량(g)</td>
<td>칼로리(kcal)</td>
<td>단백질(g)</td>
<td>지방(g)</td>
<td>탄수화물(g)</td>
<td>나트륨(mg)</td>
</tr>
`
for (var i = 0; i < num; i++) {
tag += `
<tr>
<td>${foodInfo[list[i]].name}</td>
<td>${foodInfo[list[i]].type}</td>
<td>${foodInfo[list[i]].company}</td>
<td>${foodInfo[list[i]].servingSize}</td>
<td>${foodInfo[list[i]].kcal}</td>
<td>${foodInfo[list[i]].protein}</td>
<td>${foodInfo[list[i]].fat}</td>
<td>${foodInfo[list[i]].carbohydrate}</td>
<td>${foodInfo[list[i]].natrium}</td>
</tr>
`
}
tag += `</table>
<style>
table{
border-collapse: collapse;
}
td{
border:1px solid black;
}
</style>
`;
return tag;
}
exports.foodInfo_search = function (request, response) {
var body = '';
request.on('data', function (data) {
body = body + data;
});
request.on('end', function () {
var post = qs.parse(body);
db.query(`SELECT * FROM fooddb`, function (error, foodInfo) {
var title = '식품 영양정보';
var description = '입력한 음식의 영양 정보(칼로리, 단백질, 지방, 탄수화물, 나트륨)를 알려드립니다!';
var search = post.search; // 검색어
var num = 0; // 검색된 개수
var foodName = '';
var list = [];
//console.log(foodInfo[0]);
for (var i = 0; i < foodInfo.length; i++) {
foodName = foodInfo[i].name;
if (foodName.indexOf(search) >= 0) {
list.push(i);
num += 1;
}
}
var html = template.html(title, `
<div style= "font-size:25px; background-color:#ffffff">
<h2><font color="black">${title}<div style= "font-size:10px;"><br><div style= "font-size:20px;">${description}</font></div></h2>
<form action="/foodInfo_search" method="post">
<input type = 'text' name = 'search' placeholder = '검색어 입력' maxlength = 255 value = "" autocomplete = "off" style="width:300px;height:20px;font-size:20px;">
<button type = "submit" name = "click" style="font-size:20px;">검색</button>
</form>
<p style= "font-size:20px;">${search}(으)로 검색된 결과 : ${num}개</p>
</div>
${foodTable(foodInfo, list, num)}
<br><br>
`);
response.writeHead(200);
response.end(html);
});
});
}