-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (74 loc) · 3.56 KB
/
index.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
//Add requires
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const chalk = require('chalk');
const yelp = require('yelp-fusion');
const app = express();
//Set global variables
let port = process.env.PORT || 8080;
const yelpClient = yelp.client('h04jR83l6ypfTbuAA7IqQmVgMTnhbgMkALkyGapX6W57ez-ODD2okL7PgOqc9Gk30IEDGbkjUSu8X7lRbB6T7MCMZCr_mOwkbl11w2xM8nr6z_QAL-dErj3-KJu4W3Yx');
//Allow app to use Public directory for CSS
app.use(express.static(__dirname + '/public'));
//Allow app to use body-parser
app.use(bodyParser.urlencoded({extended: true}));
//Set view engine to EJS
app.set('view engine', 'ejs');
//Homepage render
app.get('/', function(req, res) {
res.render('pages/home.ejs');
});
//Find route
app.post('/find', function(req, res) {
//Should format the postcode to remove spaces etc
res.redirect('/find/' + req.body.postcode);
});
//Postcode render
app.get('/find/:postcode', function(req, res) {
//Save postcode parameter in URL
var urlParamPostcode = (req.params.postcode);
console.log(chalk.blue(urlParamPostcode));
request('https://api.postcodes.io/postcodes/' + urlParamPostcode, function (error, response, body) {
postcodeAPIResponse = JSON.parse(body);
//console.log('error:', error);
//console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
//console.log('body:', body);
console.log(chalk.red('Status code: ' + postcodeAPIResponse.status));
if (postcodeAPIResponse.status != '200') {
res.render('pages/home.ejs', {error: postcodeAPIResponse.status});
//Quit rest of process
return
}
console.log(chalk.yellow('Postcode quality: ' + postcodeAPIResponse.result.quality));
console.log(chalk.yellow('Lat: ' + postcodeAPIResponse.result.latitude));
console.log(chalk.yellow('Long: ' + postcodeAPIResponse.result.longitude));
yelpClient.search({
latitude: postcodeAPIResponse.result.latitude,
longitude: postcodeAPIResponse.result.longitude,
categories: 'restaurants',
//locale: 'en_GB',
radius: '10000',
price: '1, 2, 3, 4',
limit: '50', //Limit is severly affected by radius
open_now: 'true',
sort_by: 'rating'
})
.then(yelpResponse => {
console.log(chalk.bgMagenta(yelpResponse.jsonBody.total + ' businesses found on Yelp in total.'));
console.log(chalk.bgMagenta(yelpResponse.jsonBody.businesses.length + ' businesses returned by API. 50 limit.'));
//console.log(chalk.magenta(yelpResponse.jsonBody.businesses[0].name));
//console.log(yelpResponse.jsonBody.businesses[0]);
//console.log(yelpResponse.jsonBody);
let randomBusinessCalc = Math.floor(Math.random() * (yelpResponse.jsonBody.businesses.length));
res.render('pages/result.ejs', {userPostcode: postcodeAPIResponse.result.postcode, businessData: yelpResponse.jsonBody, randomBusiness: randomBusinessCalc, totalBusinesses: yelpResponse.jsonBody.total});
})
.catch(error => {
console.log('Error: ' + error);
res.render('pages/home.ejs', {error: error});
});
});
});
//Listen server
app.listen(port, function() {
console.log(chalk.green('Server started and listening on port ' + port));
});