diff --git a/short.js b/short.js index a9bc29d..7e82acf 100644 --- a/short.js +++ b/short.js @@ -10,7 +10,6 @@ mongoose.connect("mongodb://localhost/short"); // TODO: Add basic auth here - app.get('/api/*', function(req, res){ if (req.url === '/favicon.ico') { return; @@ -21,17 +20,11 @@ app.get('/api/*', function(req, res){ if (error) { console.error(error); } else { - short.get(shortURL.hash, function(error, shortURLObject) { - if (error) { - console.error(error); - } else { - var URL = shortURLObject[0].URL; - var hash = shortURLObject[0].hash; - var tiny_url = "http://127.0.0.1:" + port + "/" + hash; - console.log("URL is " + URL + " " + tiny_url); - res.end(tiny_url); - }; - }); + var URL = shortURL.URL, + hash = shortURL.hash, + tiny_url = "http://127.0.0.1:" + port + "/" + hash; + console.log("URL is " + URL + " " + tiny_url); + res.end(tiny_url); } }); }); @@ -42,23 +35,23 @@ app.get('*', function(req, res) { } var hash = req.url.slice(1); - short.get(hash, function(error, shortURLObject) { - if (error) { - console.error(error); + short.get(hash, function(error, shortURLObject) { + if (error) { + console.error(error); + } else { + if (shortURLObject) { + var URL = shortURLObject[0].URL; + res.writeHead(302, { + "Location" : URL + }); + res.end(); } else { - if (shortURLObject) { - var URL = shortURLObject[0].URL; - res.writeHead(302, { - "Location" : URL - }); - res.end(); - } else { - res.writeHead(200, { "Content-Type" : "text/html" }); - res.write("URL not found!"); - res.end(); - } - }; - }); + res.writeHead(200, { "Content-Type" : "text/html" }); + res.write("URL not found!"); + res.end(); + } + }; + }); }); app.listen(port, function() { diff --git a/short/lib/short.js b/short/lib/short.js index da8f3de..a7ef2bd 100755 --- a/short/lib/short.js +++ b/short/lib/short.js @@ -25,7 +25,6 @@ short.gen = function(URL, callback) { callback(error, null); } else { if (shortenedURLs.length === 0) { - console.log(URL); var item = new ShortURL({ URL : URL, hash : hashedURL diff --git a/short/models/ShortURL.js b/short/models/ShortURL.js index 96c9605..8e8c8f9 100755 --- a/short/models/ShortURL.js +++ b/short/models/ShortURL.js @@ -14,7 +14,7 @@ var shortURL_schema = { id : ObjectId, URL : String, hash : { type : String, index: true }, - hits : { type : Number, default: -1 }, + hits : { type : Number, default: 0 }, created_at : { type : Date, default: Date.now } }; @@ -46,7 +46,7 @@ ShortURL.findByHash = function(hash, callback) { if (error) { callback(error, null); } else { - callback(null, URL); + callback(null, URL); }; }); } else {