-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrain.js
More file actions
21 lines (18 loc) · 683 Bytes
/
rain.js
File metadata and controls
21 lines (18 loc) · 683 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var app = require('express')();
var request = require('request');
var _ = require('underscore');
var weatherUrl = 'http://api.openweathermap.org/data/2.5/forecast';
app.use(require('cors')());
app.get('/rain', function(req, res){
var city = req.query.city,
country = req.query.country;
request.get({url : weatherUrl + '?q=' + city + ',' + country, json : true}, function(err, response, weatherbody){
// sum all the inches rainfall in the forecast
weatherbody.rainfall = _.reduce(weatherbody.list, function(a, b){
var b = b.rain && b.rain['3h'] || 0;
return a + b;
}, 0);
return res.json(weatherbody);
});
});
var server = app.listen(3001);