seungmi

print페이지에 음식이름이 랜덤으로 나오도록 설정

'http://localhost:3000/food/recommendation'에서 조건을 입력하였을 때,
post함수로 호출된 'http://localhost:3000/food' page에서 food.json파일안의 음식이름이 랜덤으로 호출되도록 변경
...@@ -36,7 +36,6 @@ app.get(['/food', '/food/:id'], function(req, res) { ...@@ -36,7 +36,6 @@ app.get(['/food', '/food/:id'], function(req, res) {
36 }) 36 })
37 37
38 app.post('/food', function(req, res) { 38 app.post('/food', function(req, res) {
39 -
40 fs.readFile('data/food.json', 'utf8', function(err, data) { 39 fs.readFile('data/food.json', 'utf8', function(err, data) {
41 if (err) { 40 if (err) {
42 console.log((err)); 41 console.log((err));
...@@ -46,13 +45,26 @@ app.post('/food', function(req, res) { ...@@ -46,13 +45,26 @@ app.post('/food', function(req, res) {
46 var price = req.body.price; 45 var price = req.body.price;
47 var shape = req.body.shape; 46 var shape = req.body.shape;
48 var kinds = req.body.kinds; 47 var kinds = req.body.kinds;
49 - res.render('print.ejs',{ 48 +
49 + var rand = Math.floor(Math.random() * 3); //(Math.random() * (max - min)) + min
50 + var i = 0;
51 + for (var foods in users) {
52 + if (rand == i) {
53 + var food_value=foods;
54 + // $('<li>').text(foods).appendTo('#users');
55 + }
56 + i++;
57 + }
58 +
59 + console.log(rand);
60 + res.render('print.ejs', {
50 title: 'Recommendation', 61 title: 'Recommendation',
51 description: 'We recommend this...', 62 description: 'We recommend this...',
52 - name: users[1]["name"] 63 + // name: users,
64 + randvalue: food_value
53 }); 65 });
54 } 66 }
55 - }) 67 + });
56 }) 68 })
57 69
58 app.listen(3000, function() { 70 app.listen(3000, function() {
......
1 { 1 {
2 - "1":{ 2 + "바게뜨":{
3 - "name": "바게뜨",
4 "price": 20000, 3 "price": 20000,
5 "shape": "빵", 4 "shape": "빵",
6 "kinds": "양식" 5 "kinds": "양식"
7 }, 6 },
8 - "2":{ 7 + "도시락":{
9 - "name": "도시락",
10 "price": 10000, 8 "price": 10000,
11 "shape": "밥", 9 "shape": "밥",
12 "kinds": "한식" 10 "kinds": "한식"
13 - 11 + },
12 + "스파게티":{
13 + "price": 10000,
14 + "shape": "면",
15 + "kinds": "양식"
16 + },
17 + "햄버거":{
18 + "price": 10000,
19 + "shape": "빵",
20 + "kinds": "양식"
14 } 21 }
15 } 22 }
......
...@@ -8,15 +8,16 @@ ...@@ -8,15 +8,16 @@
8 8
9 <body> 9 <body>
10 <h1><a href="/food">점심메뉴 정하기</a></h1> 10 <h1><a href="/food">점심메뉴 정하기</a></h1>
11 - <h2> 11 + <h2>
12 - <%= title %> 12 + <%= title %>
13 - </h2> 13 + </h2>
14 - <ul> 14 + <ul>
15 - <il><a href="/food/search">검색</a></il><br> 15 + <il><a href="/food/search">검색</a></il><br>
16 - <il><a href="/food/recommendation">추천</a></il> 16 + <il><a href="/food/recommendation">추천</a></il>
17 - </ul> 17 + </ul>
18 - <p><%= description %></p> 18 + <p><%= description %></p>
19 - <p><%= name %></p> 19 + <p><%= randvalue%></p>
20 +
20 <article> 21 <article>
21 </article> 22 </article>
22 </body> 23 </body>
......