ws4kp-linhanced/cors/radar.mjs

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-09-04 17:03:03 -05:00
// pass through api requests
// http(s) modules
2025-03-22 13:19:36 +01:00
import https from 'https';
2020-09-04 17:03:03 -05:00
// url parsing
2025-03-22 13:19:36 +01:00
import queryString from 'querystring';
2020-09-04 17:03:03 -05:00
// return an express router
2025-03-22 13:19:36 +01:00
const radar = (req, res) => {
2020-09-04 17:03:03 -05:00
// add out-going headers
const headers = {};
headers['user-agent'] = '(WeatherStar 4000+, ws4000@netbymatt.com)';
2023-01-06 16:26:19 -06:00
headers.accept = req.headers.accept;
2020-09-04 17:03:03 -05: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 16:26:19 -06:00
}, {});
2020-09-04 17:03:03 -05:00
let query = queryString.encode(queryParams);
2023-01-06 16:26:19 -06:00
if (query.length > 0) query = `?${query}`;
2020-09-04 17:03:03 -05:00
// get the page
2023-01-06 16:26:19 -06:00
https.get(`https://radar.weather.gov${req.path}${query}`, {
2020-09-04 17:03:03 -05:00
headers,
2023-01-06 16:26:19 -06:00
}, (getRes) => {
2020-09-04 17:03:03 -05:00
// pull some info
2023-01-06 16:26:19 -06:00
const { statusCode } = getRes;
2020-09-04 17:03:03 -05:00
// pass the status code through
res.status(statusCode);
// set headers
res.header('content-type', getRes.headers['content-type']);
2020-09-08 20:07:09 -05:00
res.header('last-modified', getRes.headers['last-modified']);
2020-09-04 17:03:03 -05:00
// pipe to response
getRes.pipe(res);
2023-01-06 16:26:19 -06:00
}).on('error', (e) => {
2020-09-04 17:03:03 -05:00
console.error(e);
});
2023-01-06 16:26:19 -06:00
};
2025-03-22 13:19:36 +01:00
export default radar;