ws4kp/cors/radar.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-09-04 22:03:03 +00:00
// pass through api requests
// http(s) modules
const https = require('https');
// url parsing
const queryString = require('querystring');
// return an express router
module.exports = (req, res) => {
// add out-going headers
const headers = {};
headers['user-agent'] = '(WeatherStar 4000+, ws4000@netbymatt.com)';
2023-01-06 22:26:19 +00:00
headers.accept = req.headers.accept;
2020-09-04 22:03:03 +00:00
// get query paramaters if the exist
const queryParams = Object.keys(req.query).reduce((acc, key) => {
// skip the paramater 'u'
if (key === 'u') return acc;
// add the paramter to the resulting object
acc[key] = req.query[key];
return acc;
2023-01-06 22:26:19 +00:00
}, {});
2020-09-04 22:03:03 +00:00
let query = queryString.encode(queryParams);
2023-01-06 22:26:19 +00:00
if (query.length > 0) query = `?${query}`;
2020-09-04 22:03:03 +00:00
// get the page
2023-01-06 22:26:19 +00:00
https.get(`https://radar.weather.gov${req.path}${query}`, {
2020-09-04 22:03:03 +00:00
headers,
2023-01-06 22:26:19 +00:00
}, (getRes) => {
2020-09-04 22:03:03 +00:00
// pull some info
2023-01-06 22:26:19 +00:00
const { statusCode } = getRes;
2020-09-04 22:03:03 +00:00
// pass the status code through
res.status(statusCode);
// set headers
res.header('content-type', getRes.headers['content-type']);
2020-09-09 01:07:09 +00:00
res.header('last-modified', getRes.headers['last-modified']);
2020-09-04 22:03:03 +00:00
// pipe to response
getRes.pipe(res);
2023-01-06 22:26:19 +00:00
}).on('error', (e) => {
2020-09-04 22:03:03 +00:00
console.error(e);
});
2023-01-06 22:26:19 +00:00
};