-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
95 lines (76 loc) · 2.38 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
var express = require('express');
var NodeGeocoder = require('node-geocoder');
var app = express();
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var port = process.env.PORT || 5000;
var bodyParser = require('body-parser');
const ejsLint = require('ejs-lint');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
// set linter
ejsLint('views/home.ejs');
// set the view engine to ejs
app.set('view engine', 'ejs')
// add middleware
app.use(function(req, res, next) {
// allow any origin to access the server
res.header("Access-Control-Allow-Origin", "*");
// indicates available HTTP response headers
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.static(__dirname + '/staticFiles'));
// ROUTES
// - Home page
app.get('/', (req, res) => {
// render `home.ejs` with the list of posts
res.render('home')
})
// post request gets user input from address form
app.post('/address', function(req, res) {
// Tester code
console.log(req.body.address);
res.send(req.body.address);
function scrape() {
url = 'http://api.dronestre.am/data';
request(url, function(error, response, body){
if(!error){
var $ = cheerio.load(body);
};
var obj = JSON.parse(body);
console.log(obj[0].country);
var options = {
noColor: false
};
fs.writeFile('output.json', body , function(err){
console.log('File successfully written! - Check your project directory for the output.json file');
})
res.send('Check the terminal')
})
}
var options = {
provider: 'google',
httpAdapter: 'https', // Default
// apiKey: 'options.clientIdand options.apiKey',
formatter: null // 'gpx', 'string', ...
};
var geocoder = NodeGeocoder(options);
// Using callback
geocoder.geocode(req.body.address, function(err, res) {
console.log(res);
// console.log(res.body.longitude);
// var res = JSON.parse(res, function(data){
// console.log(data);
// })
});
// // render the `home.ejs` template with coordinate data
// res.render('home', {
// address: home.address
// })
});
// start the server
app.listen(port, function() {
console.log('Server started! At http://localhost:' + port);
});