From a1429550e9a307cd9eaccc02289754162399d9e0 Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Thu, 5 Jun 2014 17:59:40 +0200 Subject: [PATCH 1/3] Update dependencies to express 4.4 and short 2.2 --- package.json | 4 ++-- short.js | 38 +++++++++++++++++++------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 47e2c5d..dcc367f 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "node": ">= v0.4.12 < 0.7.0" }, "dependencies": { - "express":"2.5.4", - "short":"1.3" + "express": "4.4.1", + "short": "2.2" } } \ No newline at end of file diff --git a/short.js b/short.js index 1189262..a69fa0c 100644 --- a/short.js +++ b/short.js @@ -1,7 +1,7 @@ var url = require('url'), express = require('express'), short = require('short'), - app = express.createServer(), + app = express(), port = process.env.PORT || 8080, domain = "http://192.168.1.111"; @@ -14,15 +14,13 @@ app.get('/api/*', function (req, res) { var removeApi = req.url.slice(5), URL = removeApi, options = {length: 7}; - short.generate(URL, options, function (error, shortURL) { - if (error) { - console.error(error); - } - else { - var tinyUrl = [domain, ":", port, "/", shortURL.hash].join(""); - console.log(["URL is ", shortURL.URL, " ", tinyUrl].join("")); - res.end(tinyUrl); - } + short.generate({URL: URL}, options).then(function (shortURL) { + var tinyUrl = [domain, ":", port, "/", shortURL.hash].join(""); + console.log(["URL is ", shortURL.URL, " ", tinyUrl].join("")); + res.send(tinyUrl); + }, function (error) { + console.error(error.message); + res.send(500, 'Internal server error'); }); }); @@ -33,16 +31,18 @@ app.get('*', function (req, res) { var visitor = req.connection.remoteAddress, hash = req.url.slice(1), options = {visitor: visitor}; - short.retrieve(hash, options, function (error, shortURLObject) { - if (error) {console.error(error); + short.retrieve(hash, options).then(function (shortURLObject) { + if (shortURLObject) { + res.redirect(302, shortURLObject.URL); + } else { + res.send(404, 'URL not found!'); + } + }, function (error) { + console.error(error.message); + if (error instanceof Error && error.message.indexOf('Cannot find Document') !== -1) { + res.send(404, 'URL not found'); } else { - if (shortURLObject) { - res.redirect(shortURLObject.URL, 302); - } - else { - res.send('URL not found!', 404); - res.end(); - } + res.send(500, 'Internal server error'); } }); }); From 6f349de2297ff9627919f7fa69fcc35a1789ab70 Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Thu, 5 Jun 2014 18:28:36 +0200 Subject: [PATCH 2/3] Make domain and mongo url configureable --- short.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/short.js b/short.js index a69fa0c..2a6f3a1 100644 --- a/short.js +++ b/short.js @@ -3,9 +3,10 @@ var url = require('url'), short = require('short'), app = express(), port = process.env.PORT || 8080, - domain = "http://192.168.1.111"; + domain = process.env.DOMAIN || ("http://localhost:" + port); + mongo = process.env.MONGODB || "mongodb://localhost/short"; -short.connect("mongodb://localhost/short"); +short.connect(mongo); app.get('/api/*', function (req, res) { if (req.url === '/favicon.ico') { @@ -15,7 +16,7 @@ app.get('/api/*', function (req, res) { URL = removeApi, options = {length: 7}; short.generate({URL: URL}, options).then(function (shortURL) { - var tinyUrl = [domain, ":", port, "/", shortURL.hash].join(""); + var tinyUrl = [domain, "/", shortURL.hash].join(""); console.log(["URL is ", shortURL.URL, " ", tinyUrl].join("")); res.send(tinyUrl); }, function (error) { From 78e0356adbb510242ded42ab968afc7f1389c057 Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Thu, 5 Jun 2014 18:28:46 +0200 Subject: [PATCH 3/3] Add handler for mongo error --- short.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/short.js b/short.js index 2a6f3a1..77937d4 100644 --- a/short.js +++ b/short.js @@ -7,6 +7,9 @@ var url = require('url'), mongo = process.env.MONGODB || "mongodb://localhost/short"; short.connect(mongo); +short.connection.on('error', function(error) { + throw new Error(error); +}); app.get('/api/*', function (req, res) { if (req.url === '/favicon.ico') {