-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (54 loc) · 1.75 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
var express = require('express');
var request = require('request');
var app = express();
app.set('port', process.env.PORT || 5000);
app.use(express.static('public'));
var weather_host = "http://api.wunderground.com/api/d95017df2847b211";
function weatherAPI(path, done) {
var url = weather_host + path;
console.log(url);
request({
url: url,
method: "GET",
headers: {
"Content-Type": "application/json;charset=utf-8",
"Accept": "application/json"
},
}, function(err, req, data) {
if (err) {
done(err);
} else {
if (req.statusCode >= 200 && req.statusCode < 400) {
try {
done(null, JSON.parse(data));
} catch(e) {
console.log(e);
done(e);
}
} else {
console.log(err);
done({ message: req.statusCode, data: data });
}
}
});
}
app.get('/api/weather', function(req, res) {
var yesterdayDate = new Date(new Date() - 86400000);
var yr = yesterdayDate.getFullYear();
var mo = yesterdayDate.getMonth() + 1;
var da = yesterdayDate.getDate();
if(Number(mo) < 10) mo = "0" + mo;
if(Number(da) < 10) da = "0" + da;
weatherAPI("/conditions/forecast10day/history_" + yr + mo + da + "/q/94105.json", function(err, result) {
if (err) {
console.log(err);
res.send(err).status(400);
} else {
console.log("10 days Forecast");
res.json(result);
}
});
});
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});