Skip to content

Commit

Permalink
I think I'm done
Browse files Browse the repository at this point in the history
  • Loading branch information
hogdogthegod committed Jan 10, 2017
1 parent ac2e6b7 commit e41d8a0
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/data
/data
test.js
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
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
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
124 changes: 85 additions & 39 deletions server.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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');
})

0 comments on commit e41d8a0

Please sign in to comment.