-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (47 loc) · 1.66 KB
/
index.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
var express = require("express")
var app = express();
var mongo = require("mongodb").MongoClient;
var check = require("./urlCheck.js");
var db;
var port = process.env.PORT;
app.use(express.static(__dirname + "/public"))
app.set("view engine", "ejs");
mongo.connect('mongodb://localhost:27017/benDB', (err, database) => {
if(err) return console.log(err);
db = database;
app.listen(port, function() {
console.log("Listening on port " + port);
})
})
app.get("/new/*", (req, res) => {
var urlLong = req.originalUrl.substring(5)
var urlShort = "";
urlLong = check.urlAdapt(urlLong);
// check if url already exists in database
db.collection("urls").findOne({long: urlLong}, function (err, doc){
if (err) console.log(err);
if (doc){
console.log("Entry already found");
res.render("pages/shortlink", {link: doc.short});
} else {
// The long URL was not found in the long_url field in our urls
// collection, so we need to create a new entry
if (check.urlCheck(urlLong)) {
console.log(urlLong + " returned true");
urlShort = check.genShort();
db.collection("urls").save({long: urlLong, short: urlShort}, (err, result) => {
if (err) console.log(err);
});
res.render("pages/shortlink", {link: urlShort})
} else {
res.render("pages/error")
} }
});
});
app.get("/*", (req, res)=> {
var shortLink = req.originalUrl.substring(1);
db.collection("urls").find({short: shortLink}).toArray(function(err, results) {
if (err) return console.log(err)
res.render("pages/index", {link: results})
})
});