-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
51 lines (42 loc) · 1.3 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
const express = require('express');
const yelp = require('yelp-fusion');
const app = express();
const clientId = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
app.set('port', process.env.PORT || 3001);
// Express only serves static assets in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
app.get('/api/food/:city', (req, res) => {
const city = req.params.city;
yelp.accessToken(clientId, clientSecret).then(token => {
const client = yelp.client(token.jsonBody.access_token);
client.search({
location: city,
limit: 50
}).then(results => {
res.json(results.jsonBody.businesses);
});
}).catch(e => {
console.log(e);
});
});
app.get('/api/food/:city/:type', (req, res) => {
const city = req.params.city, type = req.params.type;
yelp.accessToken(clientId, clientSecret).then(token => {
const client = yelp.client(token.jsonBody.access_token);
client.search({
term: type,
location: city,
limit: 50
}).then(results => {
res.json(results.jsonBody.businesses);
});
}).catch(e => {
console.log(e);
});
});
app.listen(app.get('port'), () => {
console.log(`Find the server at: http://localhost:${app.get('port')}/`); // eslint-disable-line no-console
});