-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
175 lines (155 loc) · 6.05 KB
/
index.js
File metadata and controls
175 lines (155 loc) · 6.05 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const express = require('express')
const app = express()
const port = 3000
const https = require('https');
const key = require('./js/donotdelete');
//const axios = require('axios');
const path = require('path');
const cacheValidity = 43200000; // for testing, once every 12 hrs, use with port 3000
//const cacheValidity = 1800000; // for real, once every 30 min, use with port 80
const apiKey = key.apiKey;
//---------------------------------------------------------------------------------------------------------
["/", "/index.html"].forEach(function(entryPoint){
app.get(entryPoint, (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});
});
app.get('^/js/:js(*.js)', (req, res) => {
res.sendFile(path.join(__dirname + '/js/' + req.params.js));
});
app.get('/weatherdata', (req, res) => {
getWeatherData(req.query.postalCode, req.query.country, function(weatherData){
res.send(weatherData);
});
});
app.get('/weatherdataforecast', (req, res) => {
getWeatherDataForecast(req.query.postalCode, req.query.country, function(weatherData){
res.send(weatherData);
});
});
app.get('^/html/:html(weather*.html)', (req, res) => {
res.sendFile(path.join(__dirname + '/html/' + req.params.html));
});
app.get('^/icon/:icon(*.png)', (req, res) => {
res.sendFile(path.join(__dirname + '/icon/' + req.params.icon));
});
app.get('^/font/:font(weather*)', (req, res) => {
res.sendFile(path.join(__dirname + '/font/' + req.params.font));
});
app.get('^/mockData/:json(*.json)', (req, res) => {
res.sendFile(path.join(__dirname + '/mockData/' + req.params.json));
});
app.get('^/css/:css(*.css)', (req, res) => {
res.sendFile(path.join(__dirname + '/css/' + req.params.css));
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
//------------------------------------------------------------------------------
var cachedWeatherData = {};
var cachedWeatherDataForecast = {};
function getWeatherData(postalCode, country, callback){
let cachedData = cachedWeatherData[postalCode];
let currentTimestamp = new Date().getTime();
if(cachedData && (currentTimestamp - cachedData.ts) < cacheValidity)
{
callback(cachedData.data);
}
else
{
const currentWeatherUrl = "https://api.weatherbit.io/v2.0/current?key=" + apiKey +
"&lang=en&units=M&postal_code=" + postalCode +
"&country=" + country;
console.log("call to weatherbit for current");
https.get(currentWeatherUrl, (res) => {
var { statusCode } = res;
var contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error('Request Failed.\n' + `Status Code: ${statusCode}`);
}
else if (!/^application\/json/.test(contentType))
{
error = new Error('Invalid content-type.\n' + `Expected application/json but received ${contentType}`);
}
if (error)
{
console.error(error.message);
// consume response data to free up memory
res.resume();
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
try {
const parsedData = JSON.parse(rawData);
callback(parsedData);
// Update cache
cachedWeatherData[postalCode] = {
ts: new Date().getTime(),
data: parsedData
};
// console.log(parsedData);
} catch (e) {
console.error(e.message);
}
});
});
}
}
function getWeatherDataForecast(postalCode, country, callback)
{
let cachedData = cachedWeatherDataForecast[postalCode];
let currentTimestamp = new Date().getTime();
if(cachedData && (currentTimestamp - cachedData.ts) < cacheValidity)
{
callback(cachedData.data);
}
else
{
const weatherForecastUrl = "https://api.weatherbit.io/v2.0/forecast/daily?key=" + apiKey +
"&lang=en&units=M&postal_code=" + postalCode +
"&country=" + country +
"&days=4";
console.log("call to weatherbit for forecast");
https.get(weatherForecastUrl, (res) => {
var { statusCode } = res;
var contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error('Request Failed.\n' +
`Status Code: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error('Invalid content-type.\n' +
`Expected application/json but received ${contentType}`);
}
if (error) {
console.error(error.message);
// consume response data to free up memory
res.resume();
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
try {
const parsedData = JSON.parse(rawData);
callback(parsedData);
// Update cache
cachedWeatherDataForecast[postalCode] = {
ts: new Date().getTime(),
data: parsedData
};
// console.log(parsedData);
} catch (e) {
console.error(e.message);
}
});
});
}
}