-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathapp.js
112 lines (102 loc) · 3.58 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
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
101
102
103
104
105
106
107
108
109
110
111
// app.js
//*************************************************************************
// Copyright 2016 IBM Corp.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*************************************************************************
var express = require('express');
var request = require('request');
var cfenv = require('cfenv');
//Security - helmet
var helmet = require('helmet');
//setup middleware
var app = express();
var ninetyDaysInMilliseconds = 7776000000;
app.configure(function(){
app.use(express.static(__dirname + '/public'));
// set the HTTP Strict Transport Security (HSTS) header for 90 days
app.use(helmet.hsts({
maxAge: ninetyDaysInMilliseconds,
includeSubdomains: true,
force: true
}));
// Prevent Cross-site scripting (XSS) attacks
app.use(helmet.xssFilter());
});
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
var weather_host = appEnv.services["weatherinsights"]
? appEnv.services["weatherinsights"][0].credentials.url // Weather credentials passed in
: ""; // or copy your credentials url here for standalone
function weatherAPI(path, qs, done) {
var url = weather_host + path;
console.log(url, qs);
request({
url: url,
method: "GET",
headers: {
"Content-Type": "application/json;charset=utf-8",
"Accept": "application/json"
},
qs: qs
}, 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/forecast/daily', function(req, res) {
var geocode = (req.query.geocode || "45.43,-75.68").split(",");
weatherAPI("/api/weather/v1/geocode/" + geocode[0] + "/" + geocode[1] + "/forecast/daily/10day.json", {
units: req.query.units || "m",
language: req.query.language || "en"
}, function(err, result) {
if (err) {
console.log(err);
res.send(err).status(400);
} else {
console.log("10 days Forecast");
res.json(result);
}
});
});
app.get('/api/forecast/hourly', function(req, res) {
var geocode = (req.query.geocode || "45.43,-75.68").split(",");
weatherAPI("/api/weather/v1/geocode/" + geocode[0] + "/" + geocode[1] + "/forecast/hourly/48hour.json", {
units: req.query.units || "m",
language: req.query.language || "en"
}, function(err, result) {
if (err) {
res.send(err).status(400);
} else {
console.log("24 hours Forecast");
result.forecasts.length = 24; // we require only 24 hours for UI
res.json(result);
}
});
});
app.listen(appEnv.port, appEnv.bind, function() {
console.log("server starting on " + appEnv.url);
});