-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
87 lines (69 loc) · 2.35 KB
/
server.js
File metadata and controls
87 lines (69 loc) · 2.35 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
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
const express = require('express');
const app = express();
const fetch = require('node-fetch')
const API_KEY = require('./api_key.js');
const four_square_client_key = require('./four_square_client_key.js');
const four_square_secret_key = require('./four_square_secret_key.js');
const GEO_KEY = require('./geo_key.js');
var port = process.env.PORT || 8080;
app.use(express.static('client/public'))
app.use(express.static('client/src'))
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};
app.use(allowCrossDomain);
app.get('/', function(req, res){
res.sendFile('index.html');
});
// route for darksky data overcoming CORS issue
app.get('/weather/:location/:date' , function(req, res){
const location = req.params.location;
const date = req.params.date;
const url = `https://api.darksky.net/forecast/${API_KEY}/${location},${date}`;
console.log(url);
fetch(url)
.then(res => res.json())
.then(data => res.json(data))
.catch((err) =>{
console.log(err);
})
})
// route for geograph map api
app.get('/longlat/:input/' , function(req, res){
const input = req.params.input;
var matches = input.match(/\d+/g);
if (matches === null){
var url = `http://api.geograph.org.uk/syndicator.php?key=[GEO_KEY]&text=${input}&format=JSON`
}else{
var url = `http://api.geograph.org.uk/syndicator.php?key=[GEO_KEY]&location=${input}&format=JSON`;
}
fetch(url)
.then(res => res.json())
.then(data => res.json(data))
.catch((err) =>{
console.log(err);
})
})
app.get('/hotel/:location' , function(req, res){
const location = req.params.location;
const url = `https://api.foursquare.com/v2/venues/search?client_id=${four_square_client_key}&client_secret=${four_square_secret_key}&v=20180323&limit=10&ll=${location}&query=hotel`;
console.log(url);
fetch(url)
.then(res => res.json())
.then(data => res.json(data))
.catch((err) =>{
console.log(err);
})
})
app.listen(port, function(){
console.log('The app is running on http://localhost:' + port)
});