Skip to content

Instantly share code, notes, and snippets.

@ericnakagawa
Created July 10, 2015 17:52
Show Gist options
  • Save ericnakagawa/3a35018036c93a989c29 to your computer and use it in GitHub Desktop.
Save ericnakagawa/3a35018036c93a989c29 to your computer and use it in GitHub Desktop.
app.get('/:url', function(req, res) {
var Link = Parse.Object.extend("Link");
var url = req.params.url;
var query1 = new Parse.Query(Link);
query1.equalTo("shorturl", req.params.url);
var query2 = new Parse.Query(Link);
query2.equalTo("longurl", req.params.url);
var query3 = new Parse.Query(Link);
query3.equalTo("longhyphenurl", req.params.url);
var mainQuery = Parse.Query.or(query1, query2, query3);
mainQuery.descending("createdAt");
mainQuery.first().then(function(link){
if(link) {
link.increment("views");
link.save().then(function(success) {
res.redirect(link.get("url"));
}, function(error) {
res.redirect(link.get("url"))
});
} else {
res.redirect("/");
}
}, function(error) {
res.send("error");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment