pull/47/head
HardiReady 2018-10-20 20:06:13 +02:00
parent d3b5571022
commit c0b3e27d18
1 changed files with 33 additions and 34 deletions

View File

@ -2,8 +2,8 @@
// modules
const express = require('express');
const http = require("http");
const https = require("https");
const http = require('http');
const https = require('https');
const parse = require('node-html-parser').parse;
// HTTP status codes by name
@ -13,28 +13,27 @@ const routerHandling = require('../middleware/router-handling');
/**
* getHtml: REST get request returning HTML page response
* @param options: http options object
* @param onResult: callback to pass the results JSON object(s) back
* @param {object} options: http options object
* @param {function} onResult: callback to pass the results JSON object(s) back
*/
const getHtml = (options, onResult) => {
let port = (options.port === 443) ? https : http;
let req = port.request(options, (res) => {
var output = '';
console.log(options.host + ':' + res.statusCode);
let output = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
res.on('data', (chunk) => {
output += chunk;
});
res.on('end', function() {
res.on('end', () => {
if (res.statusCode === 301) { // follow redirect
const location = res.headers.location;
const baseUrl = ((options.port === 443) ? 'https':'http') + '://' + options.host;
const baseUrl = ((options.port === 443) ? 'https' : 'http') + '://' + options.host;
options.path = location.replace(baseUrl, '');
getHtml(options, (status, redirectData) => {
onResult(status, redirectData);
})
});
} else {
onResult(res.statusCode, output);
}
@ -47,7 +46,7 @@ const defaulUserReqOptions = {
host: 'opt4.net',
port: 443,
path: '/dashboard/index.php?user/',
method: 'GET'
method: 'GET',
};
const slotting = new express.Router();
@ -69,7 +68,7 @@ slotting.route('/user/:id')
const root = parse(targetRes);
const userNameEl = root.querySelector('.contentTitle');
let user = {
name: userNameEl.childNodes[0].rawText.trim()
name: userNameEl.childNodes[0].rawText.trim(),
};
res.locals.items = user;