opt-cc/server/routes/slotting.js

93 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2018-10-20 20:00:36 +02:00
'use strict';
// modules
const express = require('express');
2018-10-20 20:06:13 +02:00
const http = require('http');
const https = require('https');
2018-10-20 20:00:36 +02:00
const parse = require('node-html-parser').parse;
// HTTP status codes by name
const codes = require('./http-codes');
const routerHandling = require('../middleware/router-handling');
/**
* getHtml: REST get request returning HTML page response
2018-10-20 20:06:13 +02:00
* @param {object} options: http options object
* @param {function} onResult: callback to pass the results JSON object(s) back
2018-10-20 20:00:36 +02:00
*/
const getHtml = (options, onResult) => {
let port = (options.port === 443) ? https : http;
let req = port.request(options, (res) => {
2018-10-20 20:06:13 +02:00
let output = '';
2018-10-20 20:00:36 +02:00
res.setEncoding('utf8');
2018-10-20 20:06:13 +02:00
res.on('data', (chunk) => {
2018-10-20 20:00:36 +02:00
output += chunk;
});
2018-10-20 20:06:13 +02:00
res.on('end', () => {
2018-10-20 20:00:36 +02:00
if (res.statusCode === 301) { // follow redirect
const location = res.headers.location;
2018-10-20 20:06:13 +02:00
const baseUrl = ((options.port === 443) ? 'https' : 'http') + '://' + options.host;
2018-10-20 20:00:36 +02:00
options.path = location.replace(baseUrl, '');
getHtml(options, (status, redirectData) => {
onResult(status, redirectData);
2018-10-20 20:06:13 +02:00
});
2018-10-20 20:00:36 +02:00
} else {
onResult(res.statusCode, output);
}
});
2018-10-20 20:33:36 +02:00
res.on('error', (err) => {
onResult(500, err);
});
2018-10-20 20:00:36 +02:00
});
req.end();
};
const defaulUserReqOptions = {
host: 'opt4.net',
port: 443,
path: '/dashboard/index.php?user/',
2018-10-20 20:06:13 +02:00
method: 'GET',
2018-10-20 20:00:36 +02:00
};
const slotting = new express.Router();
// routes **********************
slotting.route('/user/:id')
2018-10-20 20:06:13 +02:00
.get((req, res, next) => {
const userId = req.params.id;
const options = Object.assign({}, defaulUserReqOptions);
options.path = options.path.concat(userId);
2018-10-20 20:00:36 +02:00
2018-10-20 20:06:13 +02:00
getHtml(options, (status, targetRes) => {
if (status !== codes.success) {
const err = new Error('Can not resolve user from remote service');
err.status = codes.notfound;
return next(err);
}
2018-10-20 20:00:36 +02:00
2018-10-20 20:06:13 +02:00
const root = parse(targetRes);
const userNameEl = root.querySelector('.contentTitle');
let user = {
name: userNameEl.childNodes[0].rawText.trim(),
};
2018-10-20 20:00:36 +02:00
2018-10-20 20:06:13 +02:00
res.locals.items = user;
res.locals.processed = true;
return next();
});
})
2018-10-20 20:00:36 +02:00
2018-10-20 20:06:13 +02:00
.all(
routerHandling.httpMethodNotAllowed
);
2018-10-20 20:00:36 +02:00
// 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
slotting.use(routerHandling.emptyResponse);
module.exports = slotting;