-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
264 lines (211 loc) · 7.49 KB
/
app.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const express = require('express')
const app = express()
const spawn = require("child_process").spawn;
require('dotenv').config()
const mongoURL = process.env.KEY;
var bodyParser=require("body-parser");
var MongoClient = require('mongodb').MongoClient;
var fs = require("fs"),
json;
app.set('view engine', 'ejs')
// -------------------------
// Connect to the db
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json());
var dbo;
MongoClient.connect(mongoURL, function(err, db) {
if (err) throw err;
dbo = db.db("food");
});
app.get('/', (req, res) => {
// get data
dbo.collection("records").find({}).toArray(function(err, result) {
if (err) throw err;
// Make output readable
text = JSON.stringify(result)
jason = JSON.parse(text);
res.render("index")
});
})
// -------------------------
// Post to client
app.post('/getdata', (req, res) => {
// get data
dbo.collection("records").find({}).toArray(function(err, result) {
if (err) throw err;
// console.log("I got here!")
res.setHeader("Content-Type", "application/json");
// Make output readable
res.end(JSON.stringify(result));
// console.log(JSON.stringify(result));
});
})
app.post('/expiry', function(req, res){
var data = req.body; //prints john
// var id = data["_id"]
var mongo = require('mongodb');
var o_id = new mongo.ObjectId(data["_id"]);
const updateDoc = {
$set: {
"Expiry Date" : data["Expiry Date"]
},
};
// // console.log(filter)
// // console.log(updateDoc)
dbo.collection("records").updateOne({'_id': o_id}, updateDoc);
console.log("donezo")
});
app.post('/getrecipes', (req, res) => {
const PythonShell = require('python-shell').PythonShell;
function getIngredients() {
// getIngredients returns a promise that resolves to an ingredients array
return new Promise((resolve, reject) => {
var ingredients = [];
dbo.collection("records").find({}).project({"_id":0, "Item":1}).toArray(function(err, readout) {
readout.forEach(function (arrayElement) {
ingredients.push(arrayElement.Item)
})
options = {
args : ingredients.join(', ')
}
// console.log(options)
resolve(options);
})
});
}
function getRecipes(arg) {
return new Promise((resolve, reject) => {
console.log(arg)
PythonShell.run('./words2vec_rec.py', arg, function (err, result) {
if (err) throw err;
resolve(); // resolve the empty promise
});
})
}
function readJsonFileSync(filepath, encoding){
if (typeof (encoding) == 'undefined'){
encoding = 'utf8';
}
var file = fs.readFileSync(filepath, encoding);
return JSON.parse(file);
}
function getConfig(file){
var filepath = __dirname + '/' + file;
return readJsonFileSync(filepath);
}
const ingredients = getIngredients();
ingredients
.then(getRecipes)
// .then(function (responseObj) {
// return JSON.stringify(responseObj)
// })
.then(function () {
file = getConfig('sample.json')
// var string = JSON.parse(data)
// console.log(string)
res.send(file);
});
});
// function getIngredients() {
// var ingredients = [];
// dbo.collection("records").find({}).project({"_id":0, "Item":1}).toArray(function(err, readout) {
// readout.forEach(function (arrayElement) {
// ingredients.push(arrayElement.Item)
// // console.log("Hello from inside getIngredients")
// // console.log(ingredients)
// // console.log(JSON.stringify(ingredients))
// // console.log(ingredients)
// // delay(1000).then(() => console.log('ran after 1 second1 passed'));
// })
// })
// // return options = {
// // args: arg
// // };
// return ingredients;
// };
// });
// app.post('/getrecipes', (req, res) => {
// // get recipes
// // function delay(time) {
// // return new Promise(resolve => setTimeout(resolve, time));
// // }
// function readJsonFileSync(filepath, encoding){
// if (typeof (encoding) == 'undefined'){
// encoding = 'utf8';
// }
// var file = fs.readFileSync(filepath, encoding);
// return JSON.parse(file);
// }
// function getConfig(file){
// var filepath = __dirname + '/' + file;
// return readJsonFileSync(filepath);
// }
// // var ingredients2 = [];
// const PythonShell = require('python-shell').PythonShell;
// // let test = true;
// function getIngredients() {
// var ingredients = [];
// dbo.collection("records").find({}).project({"_id":0, "Item":1}).toArray(function(err, readout) {
// readout.forEach(function (arrayElement) {
// ingredients.push(arrayElement.Item)
// console.log("Hello from inside getIngredients")
// console.log(ingredients)
// // console.log(JSON.stringify(ingredients))
// // console.log(ingredients)
// // delay(1000).then(() => console.log('ran after 1 second1 passed'));
// })
// })
// // return options = {
// // args: arg
// // };
// return ingredients;
// };
// // getIngredients();
// // console.log(arg);
// function getRecipes(arg) {
// // console.log(arg)
// PythonShell.run('words2vec_rec.py', arg, function (err) {
// if (err) throw err;
// console.log('finished');
// });
// }
// function sendRecipes() {
// json = getConfig('sample.json');
// res.send(json)
// console.log("finito completo")
// }
// async function waiter() {
// const arg = await getIngredients();
// console.log("hello from here 123123")
// console.log(arg)
// getRecipes(arg);
// }
// async function waitress() {
// const output = await waiter();
// console.log(output)
// sendRecipes();
// }
// waitress();
// // sendRecipes();
// // // console.log(options)
// // PythonShell.run('words2vec_rec.py', options, function (err) {
// // if (err) throw err;
// // json = getConfig('sample.json');
// // res.send(json);
// // console.log('finished');
// // });
// // async function asyncCall() {
// // console.log('calling');
// // await cabbage();
// // json = getConfig('sample.json');
// // res.send(json);
// // console.log("success!")
// // // console.logresult);
// // // expected output: "resolved"
// // }
// // await asyncCall();
// // // console.log(json)
// // fix this!
// });
// Makes local port that enables rapid prototyping
app.listen(process.env.PORT || 5000);