app.js
4.53 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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var fs = require("fs");
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(bodyParser.urlencoded({
extended: false
})); //post에서bodyparser로 받기 위함
app.set('views', __dirname + '/views');
app.set('views engine', 'ejs');
//main page
app.get(['/food', '/food/:id'], function(req, res) {
var id = req.params.id;
if (id) {
if (id == 'search') {
res.render('map.ejs', {
title: 'Searching',
searching: '경희대학교 국제캠퍼스'
});
} else if (id == 'recommendation') {
res.render('recommendation.ejs', {
title: 'Recommendation',
description: 'Enter the conditions you want.'
});
}
} else {
res.render('index.ejs', {
title: 'Welcome',
description: 'Hello, Here is our homepage.'
});
}
})
//사용자가 호출한 search page
app.post('/food/search', function(req, res) {
var title = req.body.title;
res.render('map.ejs', {
title: 'Search',
searching: title
});
// res.redirect("https://www.google.com/search?q=" + title + "&oq=" + title + "&aqs=chrome..69i57j0l3j69i60j69i61.3149j0j9&sourceid=chrome&ie=UTF-8")
})
//사용자가 호출한 recommendation page
app.post('/food/recommendation', function(req, res) {
fs.readFile('data/food.json', 'utf8', function(err, data) {
if (err) {
console.log((err));
res.status(500).send('Internal Server Error');
} else {
var users = JSON.parse(data);
var price = req.body.price;
var shape = req.body.shape;
var kinds = req.body.kinds;
if (!Array.isArray(shape) && shape) {
var shape = [];
shape.push(req.body.shape);
}
if (!Array.isArray(kinds) && kinds) {
var kinds = [];
kinds.push(req.body.kinds);
}
// var len_shape=shape.length;
// var len_kinds=kinds.length;
var set1 = new Set();
var set2 = new Set();
var set3 = new Set();
function add(users, price, shape, kinds, callback) {
for (var foods in users) {
if (shape) {
for (var i = 0; i < shape.length; i++) {
if (users[foods]['shape'] == shape[i]) {
set1.add(foods);
}
}
} else {
set1.add(foods);
}
if (kinds) {
for (var j = 0; j < kinds.length; j++) {
if (users[foods]['kinds'] == kinds[j]) {
set2.add(foods);
}
}
} else {
set2.add(foods);
}
if (!price || (price&&users[foods]['price'] <= Number(price)+5000 && users[foods]['price'] >= Number(price)-5000)) {
set3.add(foods);
console.log(users[foods]['price']);
}
}
console.log(shape);
console.log(kinds);
console.log(set1);
console.log(set2);
console.log(set3);
// console.log('shapeleng:' + shape.length);
// console.log('kinds leng:' + kinds.length);
callback(set1, set2, set3);
}
add(users, price, shape, kinds, function(set1, set2, set3) {
let difference1 = new Set([...set1].filter(i => set2.has(i)));
let difference2 = new Set([...difference1].filter(i => set3.has(i)));
var list = {
food: [...difference2]
};
fs.writeFile('data/temp.json', JSON.stringify(list), "utf8", function(err) {
if (err) {
console.log(err);
res.status(500).send('Internal Server Error');
} else {
fs.readFile('data/temp.json', 'utf8', function(err, data) {
if (err) {
console.log((err));
res.status(500).send('Internal Server Error');
} else {
var food = JSON.parse(data)['food'];
var length = food.length;
var rand = Math.floor(Math.random() * length); //(Math.random() * (max - min)) + min
var food_value = food[rand];
console.log(rand);
res.render('print.ejs', {
title: 'Recommendation',
description: 'We recommend this...',
randvalue: food_value
}); //rander closed
}
}); //readFile closed
}
}); //writeFile closed
}); //add closed
}
}); //readFile closed
}); //post closed
app.listen(3000, function() {
console.log('Connected, 3000');
})