-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
95 lines (84 loc) · 3.31 KB
/
server.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
var express = require("express");
var mongoose = require("mongoose");
var app = express();
var cors = require("cors");
var bodyParser=require("body-parser");
var http = require("https");
var PythonShell = require('python-shell');
//var pyshell = new PythonShell('teste.py', { args: ['hello', 'world','tete']});
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
var City = require('./db/db_cities_model');
app.get("/",function(res){
res.sendfile(".../index.html");
});
/*
OMG = "countryID"+aux["location_suggestions"][0]["country_id"];
console.log("teste "+aux["location_suggestions"][0]["country_id"]);
var OMG;
PythonShell.run('teste.py',{ args: ['hello', 'world','tete']}, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
var res = results[0].replace(/u'|'/g, "\"");
var aux = JSON.parse(res);
OMG = "countryID"+aux["location_suggestions"][0]["country_id"];
console.log("teste "+aux["location_suggestions"][0]["country_id"]);
}); */
app.post("/cityquery", function(req, res) {
console.log("body is: " + req.body.city);
var cityQuery = new City({city: req.body.city});
cityQuery.save(function(err){
if(err){
console.log("Error");
}
else{
console.log("Saved city in the DB");
PythonShell.run('teste.py',{ args: [req.body.city]}, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
var response = results[0].replace(/u'|'/g, "\"");
var arr = JSON.parse(response);
res.send([arr["location_suggestions"][0]["name"], arr["location_suggestions"][0]["id"]]);
});
// res.send(req.body.city);
}});
});
app.post("/restlist",function(req,resp){
console.log("Teste "+req.body.data[1]);
PythonShell.run('listRestaurants.py',{ args: [req.body.data[1],req.body.data[0], 10]}, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results of list: %j', results);
// var response = results[0].replace(/\\"/g, "\"");
var response = results[0].replace(/, \\"/g, ", \"");
var response2 = response.replace(/\\":/g, "\":");
resp.send(JSON.parse(response2));
});
});
/*
var cityQuery = new City({city: req.body.city});
cityQuery.save(function(err){
if(err){
console.log("Error");
}
else{
console.log("Saved city in the DB");
PythonShell.run('teste.py',{ args: [req.body.city]}, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
var response = results[0].replace(/u'|'/g, "\"");
res.send(JSON.parse(response));
});
// res.send(req.body.city);
}});
*/
var server=app.listen(3000, function(){
console.log('Web server listening on port 3000');
});
server.timeout=1500;