From a5377a698ae279e12c95067446d0536b28601aa7 Mon Sep 17 00:00:00 2001 From: Fergal Moran Date: Mon, 11 Jan 2016 21:31:30 +0000 Subject: [PATCH] Fixed new API route --- server/routes.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/server/routes.js b/server/routes.js index a4fbc2f..341c681 100755 --- a/server/routes.js +++ b/server/routes.js @@ -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);