From e41d8a0da2797cebc7257c8ea384922619d294cd Mon Sep 17 00:00:00 2001 From: Hog Dog Date: Tue, 10 Jan 2017 04:30:07 +0000 Subject: [PATCH] I think I'm done --- .gitignore | 3 +- README.md | 7 ++- package.json | 3 +- server.js | 124 +++++++++++++++++++++++++++++++++++---------------- 4 files changed, 94 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 249cda9..0bf852d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/data \ No newline at end of file +/data +test.js \ No newline at end of file diff --git a/README.md b/README.md index ae10fde..e7412ef 100644 --- a/README.md +++ b/README.md @@ -7,4 +7,9 @@ User Story: If I pass an invalid URL that doesn't follow the valid http://www.ex User Story: When I visit that shortened URL, it will redirect me to my original link. -Note: all instructions and wording is taken from the example website (https://little-url.herokuapp.com/) \ No newline at end of file +Note: all instructions and wording is taken from the example website (https://little-url.herokuapp.com/) + +Other resources (no code was copied): +http://stackoverflow.com/questions/14915496/how-can-i-allow-slashes-in-my-express-routes +https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_message_headers +http://stackoverflow.com/questions/26007187/node-js-check-if-a-remote-url-exists \ No newline at end of file diff --git a/package.json b/package.json index cef0271..7d19ee9 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,6 @@ "express": "^4.14.0", "mongod": "^1.3.0", "mongodb": "^2.2.19", - "path": "^0.12.7", - "pug": "^2.0.0-beta6" + "path": "^0.12.7" } } diff --git a/server.js b/server.js index 6c69a4b..b46a40f 100644 --- a/server.js +++ b/server.js @@ -1,54 +1,96 @@ var express = require('express'); var path = require('path'); +var url = require('url'); var mongoclient = require('mongodb').MongoClient; var app = express(); app.use(express.static(path.join(__dirname, 'public'))); -app.get('/new/:url', function(req, res) { - console.log('got to our special place'); - mongoclient.connect('mongodb://localhost:27017/appdata', function(err, db){ - if (err) throw err; - console.log('opened connection to mongo'); - - //add one to the counter - db.collection('nextNum').update({}, { - $inc: {counter: 1} +app.get(/^\/new\/([:\w.\/]*)/, function(req, res) { + console.log('got to our special place: ' + req.params[0]); + + //parse, validate, and test url + var oldURL = url.parse(req.params[0]); + var h; + console.log(oldURL.protocol); + switch (oldURL.protocol) { + case 'http:': + h = require('http'); + break; + case 'https:': + h = require('https'); + break; + default: + showJSONerror(res); + return; + } + console.log('stored ' + h); + var options = { + protocol: oldURL['protocol'], + hostname: oldURL['hostname'], + method: 'HEAD' + }; + var pingreq = h.request(options, function(pingres) { + console.log('we made a ping: ' + pingres.statusCode); + if (pingres.statusCode === 200) { + mongoclient.connect('mongodb://localhost:27017/appdata', function(err, db){ + if (err) throw err; + console.log('opened connection to mongo'); + + //add one to the counter + db.collection('nextNum').update({}, { + $inc: {counter: 1} + }); + console.log('incremented db?'); + + //get the new counter value + db.collection('nextNum').find({}, {counter:1, _id:0}).toArray( function(err, data){ + if (err) throw err; + console.log('inside fn promise, current val: ' + data[0].counter); + //make new URL + var count = data[0].counter; + var newURL = 'https://shorten-url-hogdogthegod.c9users.io/' + count; + var newObj = { + 'original-url': oldURL.href, + 'short-url': newURL + }; + console.log('created object:'); + console.log(newObj); + + //output info + res.json(newObj); + console.log('returned json'); + + //save to DB + newObj['short-id'] = count; + db.collection('urls').insert(newObj); + console.log('saved to db'); + + //clean up? + res.end(); + db.close(); + console.log('cleaned up'); + }); + }) + } else { + showJSONerror(res); + } }); - console.log('incremented db?'); - - //get the new counter value - db.collection('nextNum').find({}, {counter:1, _id:0}).toArray( function(err, data){; - if (err) throw err; - console.log('inside fn promise, current val: ' + data[0].counter); - //make new URL - var count = data[0].counter; - var newURL = 'https://shorten-url-hogdogthegod.c9users.io/' + count; - var newObj = { - 'original-url': req.params.url, - 'short-url': newURL - }; - console.log('created object:'); - console.log(newObj); - - //output info - res.json(newObj); - console.log('returned json'); - //save to DB - newObj['short-id'] = count; - db.collection('urls').insert(newObj); - console.log('saved to db'); - - //clean up? - res.end(); - db.close(); - console.log('cleaned up'); - }); - }) + pingreq.on('error', function() { + showJSONerror(res) + }); + pingreq.end(); }); +function showJSONerror(res) { + console.log('caught error'); + res.json({'error': 'no such url'}); + res.end(); +} + app.get('/:id', function(req, res){ + console.log('retrieving long url'); mongoclient.connect('mongodb://localhost:27017/appdata', function(err, db) { if (err) throw err; @@ -69,6 +111,10 @@ app.get('/:id', function(req, res){ }) }); +app.get('*', function(req, res){ + showJSONerror(res); +}) + app.listen(8080, function() { console.log('listener added'); }) \ No newline at end of file