Fixed new API route

This commit is contained in:
Fergal Moran
2016-01-11 21:31:30 +00:00
parent 08732cf8bc
commit a5377a698a

View File

@@ -1,7 +1,19 @@
'use strict';
var errors = require('./components/errors');
var http = require('http');
var url = require('url');
function _parseUrl(u){
var parts = url.parse(u, true, true);
var path = parts.pathname.split('/').filter(function(arg){
return arg;
});
if (path.length == 2){
//probably a mix, reconstruct url and return
return parts.protocol + "//" + parts.host + "/mix/" + path[1];
}
return u;
}
module.exports = function (app) {
app.route('/config')
@@ -21,7 +33,7 @@ module.exports = function (app) {
app.route('/*')
.get(function (req, res) {
if (req.headers['user-agent'].indexOf('facebookexternalhit') > -1) {
var url = (app.get('apiUrl') + req.path + '/').replace(/([^:]\/)\/+/g, "$1");
var url = _parseUrl((app.get('apiUrl') + req.path + '/').replace(/([^:]\/)\/+/g, "$1"));
console.log('Api url: ' + url);
http.get(url, function (api_res) {
var body = '';
@@ -29,7 +41,7 @@ module.exports = function (app) {
body += chunk;
});
api_res.on('end', function() {
res.render('social/facebook/mix', JSON.parse(body));
res.render('social/facebook/mix', JSON.parse(body));
});
}).on('error', function (e) {
console.log("Got error: " + e.message);