Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkroth committed Nov 3, 2011
1 parent 5b7a773 commit f8db0bf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 31 deletions.
49 changes: 21 additions & 28 deletions short.js
Expand Up @@ -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;
Expand All @@ -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);
}
});
});
Expand All @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion short/lib/short.js
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions short/models/ShortURL.js
Expand Up @@ -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 }
};

Expand Down Expand Up @@ -46,7 +46,7 @@ ShortURL.findByHash = function(hash, callback) {
if (error) {
callback(error, null);
} else {
callback(null, URL);
callback(null, URL);
};
});
} else {
Expand Down

0 comments on commit f8db0bf

Please sign in to comment.