forked from Abhimanyu-Jha/Nibble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
116 lines (94 loc) · 2.98 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
//NODE MODULES
const express=require('express');
const fetch = require('node-fetch');
const bodyParser = require('body-parser');
const app=express();
var urlencodedParser = bodyParser.urlencoded({ extended: false });
// Start Express
//SERVING STATIC CONTENT
app.use('/css',express.static('css'));
app.use('/js',express.static('js'));
app.use('/images',express.static('images'));
app.use('/',express.static(__dirname));
app.use(bodyParser.json());
//SETTING THE VIEW ENGINE
app.set('view engine','ejs');
//SERVING HTML PAGES
app.get('/',function(req,res){
// {club: req.params.club, dp:req.user.thumbnail }
res.render('index',{club: '', dp:'' });
});
app.post('/search',urlencodedParser,(req,res)=>{
var details=req.body;
//SEARCH QUERY "q"
searchQuery= details.searchQuery;
// console.log(searchQuery);
var longitude= details.longitude;
// console.log(longitude);
var latitude=details.latitude;
// console.log(latitude);
//FETCH API
var minval=Number.MAX_VALUE;
var finalname;
var branch;
var arr=searchQuery.split(" ");
var q="";
for (i=0; i<arr.length; i++){
if (i!=arr.length-1){
q+=arr[i]+"+";}
else{
q+=arr[i];
}
}
// console.log(q);
//'https://developers.zomato.com/api/v2.1/search?q='+q+'&lat='+lat+'&lon='+long
fetch('https://developers.zomato.com/api/v2.1/search?q='+q+'&lat='+latitude+'&lon='+longitude, {
method: 'get',
headers: { 'Content-Type': 'application/json', 'user-key': '4d85d015e7c7d33f1f4d85edd738bfbb' },
})
.then(res => res.json())
.then(function(json){
var myarr=json.restaurants;
for (i=0;i<myarr.length;i++){
var x=myarr[i];
// console.log(x);
var a=0;
// console.log(x.restaurant.name.toLowerCase(),"blah");
if (x.restaurant.name.toLowerCase()==searchQuery){
a=Math.abs(latitude-x.restaurant.location.latitude)+Math.abs(longitude-x.restaurant.location.longitude);
if (a<minval){
minval=a;
finalname=x.restaurant.name;
branch=x.restaurant.location.locality;
}
}
}
// console.log(finalname+' '+branch);
});
//SIDHANT AND AYAAN USE FILENAME FOR RESTERAUNT NAME AND BRANCH FOR LOCATION
//FINALLY PREPARE ME THIS JSON OBJECT SIMILAR TO THIS SO THAT I CAN AUTOMATE FRONTEND
data={
restaurant_name:'Burger Singh',
location:'Kalkaji Mandir',
menu:{'Potato Crunch Burger':['description',['₹68','₹100']]},
links:['swiggy.com','zomato.com'],
photo_link:"http://www.dwarka21.com/uploads/images/burger-1.jpg"
};
//THIS SHOULD BE AT THE END
res.render('results',{data: data});
});
// app.get('/dev',function(req,res){
// // {club: req.params.club, dp:req.user.thumbnail }
// res.render('results',{data: ''});
// });
// 404 if no other route
// THIS ROUTE SHOULD BE AT THE END OF THE FILE
app.use(function (req, res) {
res.status(404).sendFile(__dirname+'/404.html');
})
// gets local IP
const ip = require("ip");
var port = process.env.PORT || 8000
app.listen(port, function() {
console.log('Running now on ' + ip.address() + ":" + port);
});