-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (66 loc) · 1.85 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
/*
Needed imports
*/
const request = require('request');
const express = require('express');
const path = require('path');
/*
Creating expresss object and setting port constant variable
*/
const app = express()
const port = 3000
// view engine setup
app.engine('pug', require('pug').__express)
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
/*
JSON format used for request and url settings
*/
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
/*
Creation of request to dataservice accuweather
---
GET request
*/
app.get('/sendWeatherAPI', (req, res) => {
var options = {
'method': 'GET',
'url': 'http://dataservice.accuweather.com/currentconditions/v1/locationKey?locationKey=115295&apikey=cdf0KORpdizmmWToY7AkI9hA0r6eQo92',
'headers': {
}
};
/*
Getting values from GET request
---
defining varaibles and obtaining data from JSON.
*/
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
const obj = JSON.parse(response.body);
var weather = obj[0].WeatherText;
var temp = obj[0].Temperature.Metric.Value;
var degrees = obj[0].Temperature.Metric.Unit;
var time = obj[0].LocalObservationDateTime;
console.log(weather);
// res.send(weather);
res.render('weather', { title: 'Weather App', weather: weather, temp: temp, degrees: degrees, time: time })
});
})
/*
Application
---
Define view and log results into console
*/
app.get('/', (req, res) => {
// var weather = "Partially Cloudy";
//res.send(weather);
res.render('index', { title: 'Weather App'})
})
/*
Logging information into console and generating URL
*/
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})