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