-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
102 lines (86 loc) · 3.52 KB
/
server.js
File metadata and controls
102 lines (86 loc) · 3.52 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
88
89
90
91
92
93
94
95
96
97
98
99
100
/*!
* Basic Weather API Application
* Thomas Boop
*/
var express = require('express');
var request = require('request');
var cfenv = require('cfenv');
var path = require('path');
var app = express();
var apiKey = '355e806e0c6b3aa9c5c24c36144d073b';
var apiEndpoint = 'http://api.openweathermap.org/data/2.5/weather';
app.set('view engine', 'jade');
//Starting page for app, redirects to /weather
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, '/public/html/home.html'));
});
//Detailed view form of application, should return a html page
app.get('/weather', function (req, res) {
getWeather(req.query.city,req.query.units,res,1);
});
//Api form of application, should return json
app.get('/weatherApi',function (req,res){
getWeather(req.query.city,req.query.units,res,0);
});
var port=process.env.VCAP_APP_PORT || 1337;
http.createServer(app).listen(port), function(){
console.log('Express server listening on port ' + port);
});
/*Sends get request to open weather map server and parses the response if it succeeds
* @param city String that contains a zip code or city name
* @param units string that is either Imperial, Standard, or Metric to denote unit
* @param res response stream to send response to
* @param detailed boolean flag (1 or 0) to show detailed html or send json
*/
function getWeather(city,units,res,detailed){
console.log('Requesting weather for: '+ city + ' with unit: ' + units);
if(isNaN(city)) //user entered a city, send to the city api
var url = apiEndpoint + '?q=' + city + '&units=' + units + '&key=' + apiKey;
else //user entered a number, assume it is a zip code and send to zip api
var url = apiEndpoint+'?zip='+ city + '&units=' + units + '&key=' + apiKey;
//Send the request
request.get(url,
function (error, response, body) {
if( !error && response.statusCode == 200) {
parseResponse(body,res,detailed);
}
else
res.send(body);
});
};
/*Parses the weather API response
* @param response response from the weather api server to parse
* @param res response stream to send response to
* @param detailed boolean flag (1 or 0) to show detailed html or send json
*/
function parseResponse(response,res, detailed){
var parsedData = JSON.parse(response);
if(parsedData.cod != 200) { //Was not able to get data for that city, just send back the server error
res.send(response);
return;
}
//display the data in json response or a detailed view based on flag
if(detailed != 1){ //simple json response
var formattedResponse= '{'
+'"lon":"' + parsedData.coord.lon
+'","lat":"' + parsedData.coord.lat
+'","city":"' + parsedData.name
+'","type":"' + parsedData.weather[0].main
+'","high":"' + parsedData.main.temp_max
+'","low":"' + parsedData.main.temp_min
+'","current":"' +parsedData.main.temp
+'","icon":"' + parsedData.weather[0].icon + '"'
+'}';
res.send(formattedResponse);
}
else //detailed view using jade
res.render(path.join(__dirname, '/public/views/weather.jade'),{ lon : parsedData.coord.lon,
lat : parsedData.coord.lat,
city : parsedData.name,
type : parsedData.weather[0].main,
high : parsedData.main.temp_max,
low : parsedData.main.temp_min,
current :parsedData.main.temp,
icon : parsedData.weather[0].icon
});
};