This commit is contained in:
Martin Donnelly 2017-09-11 12:19:14 +01:00
parent a877c92e53
commit b32cca1d48

View File

@ -9,6 +9,7 @@ const log4js = require('log4js');
const logger = log4js.getLogger();
const URL = require('url');
const http = require('http');
const https = require('https');
logger.level = 'debug';
@ -30,20 +31,36 @@ function getUrl (req, res) {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'
};
console.log('Options', options);
// console.log('Options', options);
logger.info('>> getting url');
function urlQuery (callback) {
try {
let count = 0;
http.request(options, (response) => {
http.request(options, responseHandler).end();
function responseHandler(response) {
console.log(`STATUS: ${response.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
response.setEncoding('utf8');
if (response.statusCode === 302) {
if (response.statusCode === 302 || response.statusCode === 301) {
body = [];
logger.debug('>. follow', response.headers.location, count);
const rUrl = URL.parse(response.headers.location);
rUrl.followAllRedirects = true;
rUrl.headers = {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'
};
logger.debug('>> follow', response.headers.location, count);
// logger.debug(rUrl);
count++;
request(response.headers.location);
if (rUrl.protocol === 'https:')
https.request(rUrl, responseHandler).end();
else
http.request(rUrl, responseHandler).end();
// this.request(response.headers.location);
// console.log(this);
}
let data = '';
@ -51,12 +68,14 @@ function getUrl (req, res) {
data += chunk;
});
response.on('end', () => {
callback(data);
logger.error('>> END!!', response.statusCode);
if (response.statusCode !== 302 && response.statusCode !== 301)
callback(data);
});
response.on('error', e => {
logger.error(e);
});
}).end();
}
/* http.request(options).on('response', response => {
let data = '';
@ -78,9 +97,9 @@ function getUrl (req, res) {
urlQuery(a => {
// logger.info(a);
logger.info('Got result', a);
logger.info('Got result');
// res.setHeader('Content-Type', 'application/json');
res.end(a);
res.send(a);
});
}