a little error handling

This commit is contained in:
Martin Donnelly 2018-06-13 23:41:06 +01:00
parent 381d77ac64
commit dcbcd32e53

View File

@ -29,7 +29,7 @@ const cache = apicache.middleware;
const ips = ['212.71.255.44', '82.35.75.161']; const ips = ['212.71.255.44', '82.35.75.161'];
app.use(ipfilter(ips, { 'mode': 'allow' })); // app.use(ipfilter(ips, { 'mode': 'allow' }));
app.use((err, req, res, _next) => { app.use((err, req, res, _next) => {
console.log('Error handler', err); console.log('Error handler', err);
@ -124,6 +124,7 @@ function getUrl (req, res) {
// console.log('Options', options); // console.log('Options', options);
logger.info(`>> getting ${theUrl}`); logger.info(`>> getting ${theUrl}`);
function urlQuery (callback) { function urlQuery (callback) {
const httpStart = RegExp('(http|ftp|https):\\/\\/');
try { try {
let count = 0; let count = 0;
http.request(options, responseHandler).end(); http.request(options, responseHandler).end();
@ -141,11 +142,20 @@ function getUrl (req, res) {
logger.debug('>> follow', response.headers.location, count); logger.debug('>> follow', response.headers.location, count);
count++; if (!httpStart.test(response.headers.location)) {
if (rUrl.protocol === 'https:') //
https.request(rUrl, responseHandler).end(); logger.warn('response.headers.location is not a valid url...');
else logger.info(`Failed to follow for ${theUrl}`);
http.request(rUrl, responseHandler).end(); logger.warn(response.headers.location);
logger.debug(response.headers);
}
else {
count++;
if (rUrl.protocol === 'https:')
https.request(rUrl, responseHandler).end();
else
http.request(rUrl, responseHandler).end();
}
} }
let data = ''; let data = '';