Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
janfejtek committed Oct 28, 2017
1 parent 7da78b1 commit 4f330af
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 123 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FreeCodeCamp's project URL Shortener Microservice
FreeCodeCamp's project Image Search Abstraction Layer
=========================

visit [Fertile tub on glitch.com](https://fertile-tub.glitch.me/) for online demo
visit [Pepper shallot on glitch.com](https://pepper-shallot.glitch.me/) for online demo
100 changes: 2 additions & 98 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,116 +1,20 @@
var express = require('express');
var mongodb = require('mongodb');


var app = express();
var mongoClient = mongodb.MongoClient;
var mongodburl = "mongodb://" + process.env.MONGO_NAME + ":" + process.env.MONGO_PASSWORD + "@ds229415.mlab.com:29415/" + process.env.MONGO_DBNAME;

const SHORT_LENGTH = 3;


function randomBase64() {
var random = Math.floor(Math.random() * 64);
if (random <= 25)
return String.fromCharCode(97 + random);
if (random <= 51)
return String.fromCharCode(65 - 26 + random);
if (random <= 61)
return ""+(random- 52);
switch(random) {
case 62:
return "-";
case 63:
return "_";
default:
return "=";
}
}

function generateRandomId(length) {
var generated = "";
for (var i = 0; i< length; i++) {
generated += randomBase64();
}
return generated;
}

app.get("/", function (request, response) {
response.sendFile(__dirname + '/views/index.html');
});

app.get("/shortener/*", function (request, response) {
app.get("/search*", function (request, response) {

var url = request.params[0];
if (!url.startsWith("http://www.") && !url.startsWith("https://www.")) {
response.send({error: "Wrong format"})
}
else {

mongoClient.connect(mongodburl, function(err, db) {
if (err)
throw err;

var urlsCollection = db.collection('urls');


urlsCollection.find({"url": url}).toArray(function(err, documents) {
if (err)
throw err;
var id = null;
if (documents.length == 0) {
do {
id = generateRandomId(SHORT_LENGTH);
}
while( urlsCollection.find({"id": id}).toArray(function(err, documents) {
if (documents.length > 0)
return false;
else
return true;
}));



db.collection('urls').insert({ "id": id, "url" : url }, function(err, data) {
if (err)
throw err;
response.send({short_url: "https://fertile-tub.glitch.me/"+id});
})

}
else {
response.send({short_url: "https://fertile-tub.glitch.me/"+documents[0].id})
}
})



})
}

});

app.get("/:value", function(request, response) {
var id = request.params.value;
mongoClient.connect(mongodburl, function(err, db) {
if (err)
throw err;
var urlsCollection = db.collection('urls');

urlsCollection.find({"id": id}).toArray(function(err, documents) {
if (err)
throw err;
if (documents.length === 0) {
response.send({error: "Invalid id"})

}
else {
response.redirect(documents[0].url)
}
})

})

app.get("/search/latest", function(request, response) {
})

// listen for requests :)
Expand Down
28 changes: 5 additions & 23 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!DOCTYPE html>
<html>
<head>
<title>FreeCodeCamp URL Shortener Microservice</title>
<title>FreeCodeCamp Image Search Abstraction Layer</title>
<meta name="description" content="FreeCodeCamp Challange 1">
<link id="favicon" rel="icon" href="https://glitch.com/edit/favicon-app.ico" type="image/x-icon">
<meta charset="utf-8">
Expand All @@ -16,47 +16,29 @@
</head>
<body>
<h1>
URL Shortener Microservice
Image Search Abstraction Layer
</h1>
<h2>
User stories:
</h2>
<ul>
<li>
I can pass a URL as a parameter and I will receive a shortened URL in the JSON response.
I can get the image URLs, alt text and page urls for a set of images relating to a given search string.
</li>
<li>
If I pass an invalid URL that doesn't follow the valid http://www.example.com format, the JSON response will contain an error instead.
I can paginate through the responses by adding a ?offset=2 parameter to the URL.
</li>
<li>
When I visit that shortened URL, it will redirect me to my original link.
I can get a list of the most recently submitted search strings.
</li>
</ul>
<h2>
Usage:
</h2>
<ul>
<li>
<a href="https://fertile-tub.glitch.me/shortener/https://www.google.com">https://fertile-tub.glitch.me/shortener/https://www.google.com</a>
<p>
Example response: {"short_url":"https://fertile-tub.glitch.me/djy"}
</p>
<p>
Error response: {error: <i>Error message</i>}
</p>
</li>
</ul>
<h2>
Notes:
</h2>
<ul>
<li>
Each url is shortened only once - multiple submits of the same site result in same short URL.
</li>
<li>
Id of the url has length of 3 characters, IDs are base64 encoded.
</li>
</ul>

<!-- Your web-app is https, so your scripts need to be too -->
<script src="https://code.jquery.com/jquery-2.2.1.min.js"
Expand Down

0 comments on commit 4f330af

Please sign in to comment.