stone/server-cache.js
2017-09-11 15:29:31 +01:00

116 lines
3.1 KiB
JavaScript

const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const config = require('./config');
const log4js = require('log4js');
const logger = log4js.getLogger();
const URL = require('url');
const http = require('http');
const https = require('https');
const apicache = require('apicache');
logger.level = 'debug';
// app.use(compression());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ 'extended': true }));
const cache = apicache.middleware;
app.use(cache('5 minutes'));
function bob(req, res) {
logger.debug(req.params);
res.sendStatus(200);
}
function getUrl (req, res) {
const theUrl = req.params.encoded_id;
logger.debug('Want', theUrl);
const options = URL.parse(theUrl);
options.followAllRedirects = true;
options.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'
};
// console.log('Options', options);
logger.info('>> getting url');
function urlQuery (callback) {
try {
let count = 0;
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 || response.statusCode === 301) {
body = [];
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++;
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 = '';
response.on('data', chunk => {
data += chunk;
});
response.on('end', () => {
if (response.statusCode !== 302 && response.statusCode !== 301)
callback(data);
});
response.on('error', e => {
logger.error(e);
});
}
/* http.request(options).on('response', response => {
let data = '';
response.on('data', chunk => {
data += chunk;
});
response.on('end', () => {
callback(data);
});
response.on('error', e => {
logger.error(e);
});
}).end();*/
}
catch (e) {
logger.error(e);
}
}
urlQuery(a => {
// logger.info(a);
logger.info('Got result');
// res.setHeader('Content-Type', 'application/json');
res.send(a);
});
}
app.get('/:encoded_id', getUrl);
const server = app.listen(config.port, () => {
logger.info(`Server listening on port ${config.port}`);
});