Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
49 lines (44 loc) · 1.41 KB
/
Copy pathserver.js
File metadata and controls
49 lines (44 loc) · 1.41 KB
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
const express = require('express');
const app = express();
const path = require('path');
const parser = require('body-parser');
const MongoClient = require('mongodb').MongoClient;
const createRouter = require('./helpers/create_router.js');
const Twitter = require('./helpers/twitter.js');
const GeocoderHelper = require('./helpers/geocoder_helper.js');
const publicPath = path.join(__dirname, '../client/public');
app.use(express.static(publicPath));
app.use(parser.json());
const twitter = new Twitter();
const geocoder = new GeocoderHelper();
MongoClient.connect('mongodb://localhost:27017')
.then((client) => {
const db = client.db('project_tweetdb');
const nameOfCollection = db.collection('saved_inputs');
const nameOfRouter = createRouter(nameOfCollection);
app.use('/api/inputs', nameOfRouter);
})
.catch(console.error);
app.get('/api/search-results', function (req, res) {
let searchTerm = req.query.query;
// console.log(searchTerm);
twitter.getAllSearchResultsFromLast7DaysForSearchTerm(searchTerm)
.then((data) => {
res.json(data);
})
.catch((err) => {
res.json(err);
});
});
app.get('/api/geocoder-results', function (req, res) {
geocoder.getLocationData('Edinburgh')
.then((data) => {
res.json(data);
})
.catch((err) => {
res.json(err);
});
});
app.listen(3000, function () {
console.log(`Listening on port ${this.address().port}`);
})