-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
49 lines (42 loc) · 1011 Bytes
/
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
'use strict'
const express = require('express')
const app = express()
const port = 3000
const twitter = require('./twitter-client')
/*
* GET /
* Serve front page
*/
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
/*
* GET /tweets
* REST endpoint to get tweets
* @param {String} q paris
* @param {String} latlon 48.8669576,2.3116284,5km
*/
app.get('/tweets', (req, res) => {
if (req.query.q !== null && req.query.latlon !== null && req.query.q !== '' && req.query.latlon !== '') {
twitter.get('search/tweets', {
q: req.query.q,
geocode: req.query.latlon,
count: 100
})
.then(function (tweets) {
// Filter Twitter API results with only tweets with geo field
let filteredTweets = tweets.statuses.filter(o => o.geo !== null)
res.send(filteredTweets)
})
.catch(function (error) {
res.status(500).send(error)
})
} else {
res.sendStatus(400)
}
})
app.listen(port, (err) => {
if (err) {
return console.log('Error', err)
}
})