Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions public/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ h1,h3,p {
#short {
margin-bottom: 0;
}

.redirect-notification {
text-align: center;
}
2 changes: 1 addition & 1 deletion src/dbOps.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ exports.init = function (port) {
useFindAndModify: false
});
} else {
mongoose.connect('mongodb+srv://user:' + process.env.MONGO_API_KEY + '@cluster0.1pvti.mongodb.net/urlshrt?retryWrites=true&w=majority',
mongoose.connect('mongodb+srv://user:' + process.env.MONGO_API_KEY + '@cluster0.uj7dpcf.mongodb.net/urlshort?retryWrites=true&w=majority',
// mongoose.connect('mongodb://localhost:27017/urls',
{
useNewUrlParser: true,
Expand Down
22 changes: 15 additions & 7 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,29 @@ router.get("/shrt/:idx", async (req, res) => {



// REDIRECT SHORT URLS TO DESTINATIONS
// SHOW REDIRECT, NOTIFY USER
// GET: USE SHORTENED URL
router.get("/:path", async (req, res) => {
// paths correspond to the bse 62 notation of the url's idx in the db
// either redirects to the original site, or provides and message of invalid url
console.log(req.params.path)
// paths correspond to the base 62 notation of the url's idx in the db
// either notify the user to the redirect, or provide an invalid url message
let idx = base62.decode(req.params.path);
console.log(idx)
let result = await dbOps.findOne(idx);
console.log(result)

if (result.longurl) {
let doc = await dbOps.routeUsed(idx);
res.redirect(result.longurl);
let doc = await dbOps.routeUsed(idx)
console.log(doc)
res.render("./../views/redirect.ejs", {
longurl: result.longurl,
shorturl: result.shorturl
});
} else {
res.send("John's URL Shortener: Invalid URL");
res.send("John's URL shortener: Invalid URL");
}
});
})


// export router object
module.exports = router;
64 changes: 2 additions & 62 deletions views/index.ejs
Original file line number Diff line number Diff line change
@@ -1,62 +1,4 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- local css style sheet -->
<link rel="stylesheet" href="/css/styles.css">
<!-- font awesome cdn -->
<script src="https://kit.fontawesome.com/b9a2b4fe17.js" crossorigin="anonymous"></script>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<link rel="icon" type="image/ico" href="/img/favicon.ico">
<meta property="og:image" content="https://i.imgur.com/DBOg0Zk.png">

<title>John's URL Shortener</title>
</head>

<body>

<!-- TITLE: includes navbar and intro header -->
<section id="title">
<div class="container-fluid">

<!-- NAVBAR -->
<nav class="navbar navbar-expand-sm navbar-dark">
<a class="navbar-brand" href="/">
<h2>URL Shortener</h2>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar-collapse-div" name="button">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbar-collapse-div">
<ul class="navbar-nav ml-auto">
<li class="navbar-item">
<a class="nav-link" href="https://johnodonnell.dev/">Personal Site</a>
</li>
<li class="navbar-item">
<a class="nav-link" href="https://github.com/john-odonnell">GitHub</a>
</li>
<li class="navbar-item">
<a class="nav-link"
href="https://www.linkedin.com/in/john-o%E2%80%99donnell-49739014a/?challengeId=AQFJmz0T1kazWQAAAXNzSJd5rYWCnndXosmz4u67if9Pgjlkwvq9UZTeRviq_jSPCQsXdTrLxksUzWHxXCiZxWAWhSAnm-6z2A&submissionId=41816754-d6e1-2316-0a70-f55b08e3616e">LinkedIn</a>
</li>
</ul>
</div>
</nav>

</div>
</section>
<%- include("partials/header") -%>

<!-- SHORTENING: includes input bar and output -->
<section id="urlshrt">
Expand Down Expand Up @@ -110,6 +52,4 @@
</div>
</section>

</body>

</html>
<%- include("partials/footer") -%>
3 changes: 3 additions & 0 deletions views/partials/footer.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
</body>

</html>
59 changes: 59 additions & 0 deletions views/partials/header.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- local css style sheet -->
<link rel="stylesheet" href="/css/styles.css">
<!-- font awesome cdn -->
<script src="https://kit.fontawesome.com/b9a2b4fe17.js" crossorigin="anonymous"></script>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<link rel="icon" type="image/ico" href="/img/favicon.ico">
<meta property="og:image" content="https://i.imgur.com/DBOg0Zk.png">

<title>John's URL Shortener</title>
</head>

<body>

<!-- TITLE: includes navbar and intro header -->
<section id="title">
<div class="container-fluid">

<!-- NAVBAR -->
<nav class="navbar navbar-expand-sm navbar-dark">
<a class="navbar-brand" href="/">
<h2>URL Shortener</h2>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar-collapse-div" name="button">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbar-collapse-div">
<ul class="navbar-nav ml-auto">
<li class="navbar-item">
<a class="nav-link" href="https://johnodonnell.dev/">Personal Site</a>
</li>
<li class="navbar-item">
<a class="nav-link" href="https://github.com/john-odonnell">GitHub</a>
</li>
<li class="navbar-item">
<a class="nav-link"
href="https://www.linkedin.com/in/john-o%E2%80%99donnell-49739014a/?challengeId=AQFJmz0T1kazWQAAAXNzSJd5rYWCnndXosmz4u67if9Pgjlkwvq9UZTeRviq_jSPCQsXdTrLxksUzWHxXCiZxWAWhSAnm-6z2A&submissionId=41816754-d6e1-2316-0a70-f55b08e3616e">LinkedIn</a>
</li>
</ul>
</div>
</nav>

</div>
</section>
29 changes: 29 additions & 0 deletions views/redirect.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%- include("partials/header") -%>

<!-- REDIRECTING: alert the user that they are being redirected -->
<section id="urlshrt">
<div class="container-fluid">
<h4 class="redirect-notification">
You are being redirected from
<a href="<%= shorturl %>"><%= shorturl %></a>
to
<a href="<%= longurl %>"><%= longurl %></a>
</h4>

<br>

<p class="redirect-notification">Press ENTER to follow redirect</p>

<script>
document.addEventListener("keyup", function(event) {
if (event.key === 'Enter') {
window.location.href = "<%= longurl %>";
}
});
</script>


</div>
</section>

<%- include("partials/footer") -%>