-
Notifications
You must be signed in to change notification settings - Fork 1
/
apikey.js
50 lines (46 loc) · 2.06 KB
/
apikey.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
// specify env file path within the dotenv require module
require('dotenv').config({path: 'keys.env'});
var fetch = require('isomorphic-fetch');
var express = require('express');
var app = express();
// set variables with API call URL
var weatherURL = `http://api.openweathermap.org/data/2.5/weather?units=imperial&APPID=${process.env.WEATHER_KEY}`;
var forecastURL = `http://api.openweathermap.org/data/2.5/forecast?units=imperial&APPID=${process.env.WEATHER_KEY}`;
var twitchURL = `https://api.twitch.tv/kraken/channels/`;
var twitchURLStreams = 'https://api.twitch.tv/kraken/streams/';
// enable CORS for cross-browser support
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
next();
});
// populate express get methods for JSON fetch
app.get('/weather/:lat/:lon', function (req, res) {
fetch(`${weatherURL}&lat=${req.params.lat}&lon=${req.params.lon}`)
.then(response => response.json())
.then(weather => res.send(weather))
.catch(error => res.send(error))
});
app.get('/forecast/:lat/:lon', function (req, res) {
fetch(`${forecastURL}&lat=${req.params.lat}&lon=${req.params.lon}`)
.then(response => response.json())
.then(forecast => res.send(forecast))
.catch(error => res.send(error))
});
app.get('/channels/:channel', function (req, res) {
fetch(`${twitchURL}${req.params.channel}?client_id=${process.env.TWITCH_KEY}`)
.then(response => response.json())
.then(stream => res.send(stream))
.catch(error => res.send(error))
});
app.get('/streams/:stream', function (req, res) {
fetch(`${twitchURLStreams}${req.params.stream}?client_id=${process.env.TWITCH_KEY}`)
.then(response => response.json())
.then(stream => res.send(stream))
.catch(error => res.send(error))
});
// verify server is running
app.listen(11646, function () {
console.log("I'm alive! Call me at 11646!");
});