Skip to content

Instantly share code, notes, and snippets.

Created April 24, 2015 08:39
Show Gist options
  • Save anonymous/79e2f02e4db22d8d8890 to your computer and use it in GitHub Desktop.
Save anonymous/79e2f02e4db22d8d8890 to your computer and use it in GitHub Desktop.
Picker.route('/:shortURL', function (params, request, response, next) {
if (params.shortURL === 'favicon.ico') next();
var location
, url = URLs.findOne({shortURL: params.shortURL});
// Not found
if (!url) location = Meteor.absoluteUrl() + 'url/not-found';
// For private links, go to client side to authorize the requester
else if (url.isPrivate) {
location = Meteor.absoluteUrl() + 'redirect/' + url.shortURL;
}
// For public links, record statistical data and process redirection
else {
var clientIP = (request.headers['x-forwarded-for'] || '').split(',')[0]
|| request.connection.remoteAddress
|| request.socket.remoteAddress
|| request.connection.socket.remoteAddress
, userAgent = request.headers['user-agent'];
// Record statistical data
Meteor.call('/visit/insert', url.shortURL, clientIP, userAgent);
location = url.targetURL;
}
response.writeHead(302, {'Location': location});
response.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment