Fix lint
parent
d3b5571022
commit
c0b3e27d18
|
@ -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,40 +46,40 @@ 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();
|
||||||
|
|
||||||
// routes **********************
|
// routes **********************
|
||||||
slotting.route('/user/:id')
|
slotting.route('/user/:id')
|
||||||
.get((req, res, next) => {
|
.get((req, res, next) => {
|
||||||
const userId = req.params.id;
|
const userId = req.params.id;
|
||||||
const options = Object.assign({}, defaulUserReqOptions);
|
const options = Object.assign({}, defaulUserReqOptions);
|
||||||
options.path = options.path.concat(userId);
|
options.path = options.path.concat(userId);
|
||||||
|
|
||||||
getHtml(options, (status, targetRes) => {
|
getHtml(options, (status, targetRes) => {
|
||||||
if (status !== codes.success) {
|
if (status !== codes.success) {
|
||||||
const err = new Error('Can not resolve user from remote service');
|
const err = new Error('Can not resolve user from remote service');
|
||||||
err.status = codes.notfound;
|
err.status = codes.notfound;
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
res.locals.processed = true;
|
res.locals.processed = true;
|
||||||
return next();
|
return next();
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
.all(
|
.all(
|
||||||
routerHandling.httpMethodNotAllowed
|
routerHandling.httpMethodNotAllowed
|
||||||
);
|
);
|
||||||
|
|
||||||
// this middleware function can be used, if you like or remove it
|
// this middleware function can be used, if you like or remove it
|
||||||
// it looks for object(s) in res.locals.items and if they exist, they are send to the client as json
|
// it looks for object(s) in res.locals.items and if they exist, they are send to the client as json
|
||||||
|
|
Loading…
Reference in New Issue