stone/server.js

96 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-09-11 10:13:57 +00:00
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');
2017-09-11 11:19:14 +00:00
const https = require('https');
2017-09-11 14:29:31 +00:00
const apicache = require('apicache');
2017-09-11 10:13:57 +00:00
logger.level = 'debug';
// app.use(compression());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ 'extended': true }));
2017-09-11 14:29:31 +00:00
const cache = apicache.middleware;
app.use(cache('5 minutes'));
2017-09-11 10:13:57 +00:00
function getUrl (req, res) {
const theUrl = req.params.encoded_id;
logger.debug('Want', theUrl);
const options = URL.parse(theUrl);
2017-09-11 10:50:04 +00:00
options.followAllRedirects = true;
2017-09-11 10:43:04 +00:00
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'
};
2017-09-11 10:13:57 +00:00
2017-09-11 11:19:14 +00:00
// console.log('Options', options);
2017-09-11 10:13:57 +00:00
logger.info('>> getting url');
function urlQuery (callback) {
try {
2017-09-11 10:52:17 +00:00
let count = 0;
2017-09-11 11:19:14 +00:00
http.request(options, responseHandler).end();
function responseHandler(response) {
2017-09-11 10:47:34 +00:00
console.log(`STATUS: ${response.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
response.setEncoding('utf8');
2017-09-11 11:19:14 +00:00
if (response.statusCode === 302 || response.statusCode === 301) {
2017-09-11 10:52:17 +00:00
body = [];
2017-09-11 11:19:14 +00:00
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);
2017-09-11 10:52:17 +00:00
count++;
2017-09-11 11:19:14 +00:00
if (rUrl.protocol === 'https:')
https.request(rUrl, responseHandler).end();
else
http.request(rUrl, responseHandler).end();
// this.request(response.headers.location);
// console.log(this);
2017-09-11 10:52:17 +00:00
}
2017-09-11 10:13:57 +00:00
let data = '';
response.on('data', chunk => {
data += chunk;
});
response.on('end', () => {
2017-09-11 11:19:14 +00:00
if (response.statusCode !== 302 && response.statusCode !== 301)
callback(data);
2017-09-11 10:13:57 +00:00
});
response.on('error', e => {
logger.error(e);
});
2017-09-11 11:19:14 +00:00
}
2017-09-11 10:13:57 +00:00
}
catch (e) {
logger.error(e);
}
}
2017-09-11 10:43:04 +00:00
urlQuery(a => {
2017-09-11 10:13:57 +00:00
// logger.info(a);
2017-09-11 11:19:14 +00:00
logger.info('Got result');
2017-09-11 10:13:57 +00:00
// res.setHeader('Content-Type', 'application/json');
2017-09-11 11:19:14 +00:00
res.send(a);
2017-09-11 10:13:57 +00:00
});
}
app.get('/:encoded_id', getUrl);
const server = app.listen(config.port, () => {
logger.info(`Server listening on port ${config.port}`);
});