stone/server.js
Martin Donnelly 6bb1bbc632 update
2017-09-11 11:47:34 +01:00

84 lines
2.0 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');
logger.level = 'debug';
// app.use(compression());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ 'extended': true }));
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.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 {
http.request(options, (response) => {
console.log(`STATUS: ${response.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
response.setEncoding('utf8');
let data = '';
response.on('data', chunk => {
data += chunk;
});
response.on('end', () => {
callback(data);
});
response.on('error', e => {
logger.error(e);
});
}).end();
/*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', a);
// res.setHeader('Content-Type', 'application/json');
res.end(a);
});
}
app.get('/:encoded_id', getUrl);
const server = app.listen(config.port, () => {
logger.info(`Server listening on port ${config.port}`);
});