-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
141 lines (133 loc) · 4.4 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var express = require('express');
var app = express();
var mongo_url = "mongodb://eric8ah:[email protected]:15962/short-urls";
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var assert = require('assert');
var url = require('url');
var path = require('path');
var validator = require('validator');
var protocols = { protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false };
var urlID;
var webpage;
var response;
var domain;
//function to insert URL and short number document into DB if not already there
var insertDocument = function(db, callback) {
db.collection('urls').insertOne(
{
"_id": urlID,
"URL": webpage
}, function(err, result) {
assert.equal(err, null);
console.log('Inserted a document into the urls collection');
callback();
});
};
//function to check database to see if URL already exists and pull that ID number
var findUrls = function(db, callback) {
var cursor = db.collection('urls').find({ "URL": webpage});
cursor.each(function(err, doc) {
if (err) {
return err;
} else if (doc !== null) {
console.dir(doc);
console.log('doc was found?');
urlID = doc._id;
webpage = doc.URL;
} else {
console.log('doc was not found?');
callback();
}
});
};
var findWebpage = function(db, callback) {
var cursor = db.collection('urls').find({ "_id": Number(webpage)});
cursor.each(function(err, doc) {
if (err) {
return err;
} else if (doc !== null) {
console.dir(doc);
console.log('doc was found?');
urlID = doc._id;
webpage = doc.URL;
} else {
console.log('doc was not found?');
callback();
}
});
};
//get request
app.get('/*', function(req, res) {
MongoClient.connect(mongo_url, function(err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', mongo_url);
webpage = url.parse(req.url).pathname.split('/')[1];
domain = req.get('host');
urlID = undefined;
//handle homepage request
if (webpage.length === 0) {
var index = path.join(__dirname, 'index.html');
res.sendFile(index, function(err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
else {
console.log('File was sent');
}
});
} else if (isNaN(webpage) === false) {
findWebpage(db, function() {
if(urlID !== undefined) {
console.log('Short URL exists');
response = webpage;
console.log('should redirect now to ' + response);
res.writeHead(302, {'Location': 'http://' + webpage});
res.end();
} else {
console.log('Short URL does not exist');
response = {
"error": "Short URL not in Database"
};
res.json(response);
}
});
} else if (validator.isURL(webpage, protocols) !== true) {
response = {
"error": "Invalid URL"
};
res.json(response);
} else {
//check to see if the URL from the request exists in the DB, if not insert it into DB
findUrls(db, function() {
if (urlID !== undefined) {
console.log('Valid ID');
response = {
"original_url": webpage,
"short_url": domain + '/' + urlID
};
res.json(response);
} else {
console.log('ID not Valid');
webpage = url.parse(req.url).pathname.split('/')[1];
urlID = Math.floor(Math.random()*1000)+ 2;
insertDocument(db, function() {
response = {
"original_url": webpage,
"short_url": domain + '/' + urlID
};
res.json(response);
});
}
db.close();
});
}
}
});
});
app.listen(process.env.PORT || 8080, function () {
console.log('Server has been started');
});