diff --git a/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_1.py b/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_1.py index b16725e3..6c395d11 100644 --- a/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_1.py +++ b/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_1.py @@ -1,2 +1,22 @@ """ Test Case 1 +Note that the graph should be copied into a CSV file test.csv to test your program. +Description: This test gives you a simple adjacency matrix of a graph and a value of 4 for n. +""" + +""" +graph: +0,1,1,0 +1,0,0,0 +1,0,0,1 +0,0,1,0 + +n: +2 + +Output: +1 +2 +2 +1 +""" diff --git a/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_2.py b/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_2.py index bb9473a2..7495f81c 100644 --- a/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_2.py +++ b/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_2.py @@ -1,2 +1,18 @@ """ Test Case 2 +Note that the graph should be copied into a CSV file test.csv to test your program. +Description: Fully connected graph with n=2; therefore, it is not ncolorable +""" + +""" +graph: +1,1,1 +1,1,1 +1,1,1 + +n: +2 + +Output: +Graph cannot be colored +""" diff --git a/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_3.py b/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_3.py index 59881da9..e238faab 100644 --- a/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_3.py +++ b/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_3.py @@ -1,2 +1,20 @@ """ Test Case 3 +Note that the graph should be copied into a CSV file test.csv to test your program. +Description: Fully connected graph with 3 nodes and n=3. Each node should have its own color +""" + +""" +graph: +1,1,1 +1,1,1 +1,1,1 + +n: +3 + +Output: +1 +2 +3 +""" diff --git a/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_4.py b/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_4.py index 1b445d8e..cd9e5658 100644 --- a/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_4.py +++ b/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_4.py @@ -1,2 +1,22 @@ """ Test Case 4 +Note that the graph should be copied into a CSV file test.csv to test your program. +Description: Sample graph with 4 nodes and n=3 +""" + +""" +graph: +0,1,1,1 +1,0,1,0 +1,1,0,1 +1,0,1,0 + +n: +3 + +Output: +1 +2 +3 +2 +""" diff --git a/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_5.py b/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_5.py index da82c900..08174104 100644 --- a/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_5.py +++ b/Module4_Labs/Lab5_nColorable/Student-Starter/Overall/test_case_5.py @@ -1,2 +1,22 @@ """ Test Case 5 +Note that the graph should be copied into a CSV file test.csv to test your program. +Description: Sample graph with 4 nodes and n=2 +""" + +""" +graph: +0,1,0,0 +1,0,1,1 +0,1,0,0 +0,1,0,0 + +n: +2 + +Output: +1 +2 +1 +1 +""" diff --git a/Postman_Lab_URL_Shortener/Cards/1-1-1.md b/Postman_Lab_URL_Shortener/Cards/1-1-1.md new file mode 100644 index 00000000..ec2d98bb --- /dev/null +++ b/Postman_Lab_URL_Shortener/Cards/1-1-1.md @@ -0,0 +1,12 @@ + + +We need to make sure we have everything we need set up and installed before moving on with the project: + +```Node.js +npm init + +npm i express mongoose ejs + +npm i --save-dev nodemon +``` + diff --git a/Postman_Lab_URL_Shortener/Cards/1.md b/Postman_Lab_URL_Shortener/Cards/1.md deleted file mode 100644 index 29d300c4..00000000 --- a/Postman_Lab_URL_Shortener/Cards/1.md +++ /dev/null @@ -1,9 +0,0 @@ - - -You might have used a URL shortener before, such as [bit.ly](https://bit.ly/), [goo.gl](https://goo.gl/). They are useful for shortening long URLs so that you can easily share them with your friends, family or co-workers. - -You might be wondering how these things work. That's why you will be building one yourself! - -Your shortener will not need a database system to host it. Instead, you will utilize [jsonstore.io](https://jsonstore.io/). Let's get started! - -![](https://miro.medium.com/max/1245/1*Pdw7h5X6vQQNVopIzHBG6A.jpeg) \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/10-1-1.md b/Postman_Lab_URL_Shortener/Cards/10-1-1.md deleted file mode 100644 index 92145404..00000000 --- a/Postman_Lab_URL_Shortener/Cards/10-1-1.md +++ /dev/null @@ -1,34 +0,0 @@ - - -The code to actually obtain our shortened URL is as follows: - -```javascript -var hashh = window.location.hash.substr(1) - -if (window.location.hash != "") { - $.getJSON(endpoint + "/" + hashh, function (data) { - data = data["result"]; - - if (data != null) { - window.location.href = data; - } - - }); -} -``` - - The above code will be executed whenever we put the short URL in the address bar (eg. https://shorturl.com/#abcd ). - -First, we store the hash value from the URL in the `hashh` variable. - -> *Example: if our short URL is https://shorted.com/#abcd , the value of the hash will be **#abcd.*** - -Then we check if the hash location is empty or not. If it’s not empty we send a get request to the address, `endpoint` + `hashh`. - -> *Example:* `https://www.jsonstore.io/8ba4fd855086288421f770482e372ccb5a05d906269a34da5884f39eed0418a1/abcd` - -And as usual, if everything is okay we will get the long URL from the data which is JSON array data, and from that we extract the result with `data["result"]`. - -> *The value of data will be similar to this `{"result":longurl,"ok":true}` , where the long URL is the URL you shortened.* - -The URL shortener is almost complete! Copy-paste a long URL in the input box then click the **Shorten The URL** button! Copy the link from the address bar — it’s your shortened URL! \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/10-1.md b/Postman_Lab_URL_Shortener/Cards/10-1.md deleted file mode 100644 index 91aa7015..00000000 --- a/Postman_Lab_URL_Shortener/Cards/10-1.md +++ /dev/null @@ -1,3 +0,0 @@ - - -First make sure to store the hash if it exists. If the hash location is empty, send a get request to the appropriate address. The information you need will be available through JSON. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/10.md b/Postman_Lab_URL_Shortener/Cards/10.md deleted file mode 100644 index 0360c707..00000000 --- a/Postman_Lab_URL_Shortener/Cards/10.md +++ /dev/null @@ -1,5 +0,0 @@ - - -Now comes the final step. We have our shortened URL. But no we need to make sure it gets converted back to the original URL whenever some inputs the shorter one in the address bar. - -Implement the necessary code to do this. This will not be part of a function. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/2-1-1.md b/Postman_Lab_URL_Shortener/Cards/2-1-1.md index 0d244b8a..cb11d1b5 100644 --- a/Postman_Lab_URL_Shortener/Cards/2-1-1.md +++ b/Postman_Lab_URL_Shortener/Cards/2-1-1.md @@ -1,25 +1,29 @@ - + -We’ll need only a text input box, a button, and a script tag to create our URL shortener. +Edit the `package.json` file to have this line: -First create an HTML file called `index.html`, as there is only a need for those two elements (a text input box and a button). +```json +"scripts": { + "devStart": "nodemon server.js" +``` -So let’s start adding our three main elements: +`server.js` will have a basic skeleton that will allow you to run the URL Shortener through your browser: -```html - - - - - - - -``` +```javascript +const express = require('express') +const app = express() + +app.get('/',(req, res) => { + res.render('index') +}) -The first element in our HTML file is `input` where we will type/paste our long URL. We will give it an id name of `urlinput` so it would be easy to access in the JavaScript. +app.listen(process.env.PORT || 5000); +``` -The next element is a `button`. When we click this button, our long URL will be shortened due to its `onclick` function. Inside the `shorturl()` function there will be commands necessary to shorten the URL. +Finally, your `index.ejs` file should only have one line: -At the end we have a `script` called `main.js` where all our main JavaScript code will be. The above-mentioned `shorturl()` function will be also there. +``` +Hello World +``` -So, for now, our HTML part is complete. \ No newline at end of file +This will be enough to create a simple webpage and display a message in your browser. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/2-1.md b/Postman_Lab_URL_Shortener/Cards/2-1.md deleted file mode 100644 index b8dba264..00000000 --- a/Postman_Lab_URL_Shortener/Cards/2-1.md +++ /dev/null @@ -1,12 +0,0 @@ - - -These HTML tags will be helpful: - -```html - - - - - -``` - diff --git a/Postman_Lab_URL_Shortener/Cards/2.md b/Postman_Lab_URL_Shortener/Cards/2.md deleted file mode 100644 index 765a5710..00000000 --- a/Postman_Lab_URL_Shortener/Cards/2.md +++ /dev/null @@ -1,7 +0,0 @@ - - -To get started with the shortener, we will need an actual UI to able to interact with the program. This doesn't need to be overly fancy. A simple webpage with a text input box and a button will do just fine. Create a file called `index.html` that will set this up. It has 3 requirements: - -- It need to be able to accepts URLs as input. -- The button, when clicked, should call a function named `shorturl()` that will be defined in our main script file. Give the button a name like "Shorten the URL". -- Our main script will be called `main.js`. Link this to the HTML file. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/3-1-1.md b/Postman_Lab_URL_Shortener/Cards/3-1-1.md index 52a06493..66cdc5fa 100644 --- a/Postman_Lab_URL_Shortener/Cards/3-1-1.md +++ b/Postman_Lab_URL_Shortener/Cards/3-1-1.md @@ -1,8 +1,42 @@ - + -This can be done as follows: +Your `index.ejs` should now look as follows: -```javascript -var endpoint = "https://www.jsonstore.io/8ba4fd855086288421f770482e372ccb5a05d906269a34da5884f39eed0418a1"; +```html + + + + + + + Document + + +

URL Shrinker

+
+ + + +
+ + + + + + + + + + + + + + + + +
Full URLShort URLClicks
https://www.google.com//12345610
+ + ``` +This gives us the basic form of our webpage, as well as all of the UI elements that we will need. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/3.md b/Postman_Lab_URL_Shortener/Cards/3.md deleted file mode 100644 index 3fd25f2a..00000000 --- a/Postman_Lab_URL_Shortener/Cards/3.md +++ /dev/null @@ -1,5 +0,0 @@ - - -Now it's time to start developing the Javascript file. The first step requires an **endpoint** that will allow us to store information about the URL. As previously mentioned, we will be using **jsonstore.io**. - -Go to jsonstore.io, and get the endpoint that is provided for you. Next, create a file called `main.js` and store this endpoint as a variable. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/4-1-1.md b/Postman_Lab_URL_Shortener/Cards/4-1-1.md index aee5ca10..c6cefd76 100644 --- a/Postman_Lab_URL_Shortener/Cards/4-1-1.md +++ b/Postman_Lab_URL_Shortener/Cards/4-1-1.md @@ -1,22 +1,45 @@ - + -`getrandom()` can be defined as below: +The `index.ejs` will now look like this: -```javascript -function getrandom() -{ - var random_string = Math.random().toString(32).substring(2, 5) - + Math.random().toString(32).substring(2, 5); - return random_string(); -} +```html + + + + + + + + Document + + +
+

URL Shrinker

+
+ + + +
+ + + + + + + + + + + + + + + + +
Full URLShort URLClicks
https://www.google.com//12345610
+
+ + ``` -First, we initiated a function called `getrandom`. Then we initialized a variable called `random_string` and gave it a value. - - We then called the `random` function from `Math` , `Math.random()` returns a random number between 0 (inclusive), and 1 (exclusive). - -Then we transform the returned number to a string using `toString()` and we give it an argument of 32 so that we get a proper string not a binary, hexadecimal or octal. - -Then we use `substring(2,5)` as well to slice the string and maintain the size of the string. Then again we follow the same procedure to get another chunk of a random string, and finally we add both chunks of the string using `+`. - -And don’t forget to add a `return` statement to actually return our random string. \ No newline at end of file +This is all the styling that you will do for this lab. This is just so that the final product doesn't look *completely* ugly. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/4-1.md b/Postman_Lab_URL_Shortener/Cards/4-1.md deleted file mode 100644 index e50cc9b3..00000000 --- a/Postman_Lab_URL_Shortener/Cards/4-1.md +++ /dev/null @@ -1,5 +0,0 @@ - - -Generating random numbers is easy enough, but what about random *characters*? - -HINT: Is there a way that we can generate random numbers that we can simply convert into characters? \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/4.md b/Postman_Lab_URL_Shortener/Cards/4.md deleted file mode 100644 index 07800a9b..00000000 --- a/Postman_Lab_URL_Shortener/Cards/4.md +++ /dev/null @@ -1,3 +0,0 @@ - - -Now we need some sort of random string that can be part of our shortened URL, that will be tied to the original long URL. Create a random string generator called `getrandom()` that generates a string of size 6. You can use the entire alphabet as well as numeric digits when generating a string. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/5-1-1.md b/Postman_Lab_URL_Shortener/Cards/5-1-1.md index 0038b2a3..d970c972 100644 --- a/Postman_Lab_URL_Shortener/Cards/5-1-1.md +++ b/Postman_Lab_URL_Shortener/Cards/5-1-1.md @@ -1,18 +1,26 @@ - - -This is a simple addition to our `index.html` file: - -```html - - - Simple URL Shortener - - - - - - - - + + +The `server.js` file will now look like this: + +```javascript +const express = require('express') +const mongoose = require('mongoose') +const app = express() + +mongoose.connect('mongodb://localhost/urlShortener', { + useNewUrlParser: true, useUnifiedTopology: true +}) + +app.set('view engine', 'ejs') + +app.get('/', (req, res) => { + res.render('index') +}) + +app.post('/shortUrls', (req, res) => { + +}) + +app.listen(process.env.PORT || 5000); ``` diff --git a/Postman_Lab_URL_Shortener/Cards/5.md b/Postman_Lab_URL_Shortener/Cards/5.md deleted file mode 100644 index e81cf99f..00000000 --- a/Postman_Lab_URL_Shortener/Cards/5.md +++ /dev/null @@ -1,3 +0,0 @@ - - -To be able to send and receive data through our webpage, we will need JQuery. Specifically, its Ajax libraries. Modify `index.html` and add JQuery as a source. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/6-1-1.md b/Postman_Lab_URL_Shortener/Cards/6-1-1.md index 2a292008..0d007d74 100644 --- a/Postman_Lab_URL_Shortener/Cards/6-1-1.md +++ b/Postman_Lab_URL_Shortener/Cards/6-1-1.md @@ -1,25 +1,29 @@ - + -`geturl()` can be defined as below: +The `shortUrl.js` should look like this: ```javascript -function geturl() -{ - var url = document.getElementById(“urlinput”).value; - var protocol_ok = url.startsWith("http://") || url.startsWith("https://") || url.startsWith("ftp://"); - if(!protocol_ok) - { - newurl = "http://" + url; - return newurl; - } - else - { - return url; - } -} -``` +const mongoose = require('mongoose') +const shortId = require('shortid') -In the `geturl` function, we first store the value of the input box in the `url` variable. Then we check if the URL protocols are OK or not. If the protocol doesn’t start with `http://` , `https://` or `ftp://` we add `http://` at the beginning of the URL then return the URL. +const shortUrlSchema = new mongoose.Schema({ + full: { + type: String, + required: true + }, + short: { + type: String, + required: true, + default: shortId.generate + }, + clicks: { + type: Number, + required: true, + default: 0 + } +}) -> NOTE: In reality, this is unsafe! We should be using regular expressions to validate our URLS, but for simplicity's sake we will choose to do it this way. +module.exports = mongoose.model('ShortUrl', shortUrlSchema) +``` +With this, we have the entire framework of what is expected in our database regarding the full URL, short URL, and the amount of times each URL was clicked. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/6-1.md b/Postman_Lab_URL_Shortener/Cards/6-1.md deleted file mode 100644 index 072c094b..00000000 --- a/Postman_Lab_URL_Shortener/Cards/6-1.md +++ /dev/null @@ -1,3 +0,0 @@ - - -Don't overthink this. The most common way that a URL wouldn't be valid is that it has an incorrect protocol identifier. Just making sure we can account for this in `geturl()` will be enough. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/6.md b/Postman_Lab_URL_Shortener/Cards/6.md deleted file mode 100644 index 1a8c4e2c..00000000 --- a/Postman_Lab_URL_Shortener/Cards/6.md +++ /dev/null @@ -1,5 +0,0 @@ - - -After we receive our URL input, we need to verify that it's a valid link. - -Create a function called `geturl()` which will take the value from the input box, verify it, and return the value. If the URL isn't valid, add logic to make it valid. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/7-1-1.md b/Postman_Lab_URL_Shortener/Cards/7-1-1.md index cf417fb5..34241e23 100644 --- a/Postman_Lab_URL_Shortener/Cards/7-1-1.md +++ b/Postman_Lab_URL_Shortener/Cards/7-1-1.md @@ -1,17 +1,32 @@ - + -`genhash()` will be defined as follows: +The `server.js` file should now look like this: -``` -function genhash() -{ - if (window.location.hash == “”) - { - window.location.hash = getrandom(); - } -} -``` +```javascript +const express = require('express') +const mongoose = require('mongoose') +const ShortUrl = require('./models/shortUrl') +const app = express() + +mongoose.connect('mongodb://localhost/urlShortener', { + useNewUrlParser: true, useUnifiedTopology: true +}) + +app.set('view engine', 'ejs') + +app.use(express.urlencoded({ extended: false })) -At first, we check if the hash location is empty. If it us, we then add a random hash to the location bar. Otherwise, we keep the original hash value. +app.get('/', (req, res) => { + res.render('index', { shortUrls: shortUrls }) +}) + +app.post('/shortUrls', async (req, res) => { + await ShortUrl.create({ full: req.body.fullUrl }) + + res.redirect('/') +}) + + +app.listen(process.env.PORT || 5000); +``` -> EXAMPLE : if our URL is https://example.com/#abcd then the value of `window.location.hash` will be `#abcd`. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/7-1.md b/Postman_Lab_URL_Shortener/Cards/7-1.md deleted file mode 100644 index b1e4e5f5..00000000 --- a/Postman_Lab_URL_Shortener/Cards/7-1.md +++ /dev/null @@ -1,3 +0,0 @@ - - -You only need to create a hash if there wasn't one already. So accont for the situation where the hash is empty. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/7.md b/Postman_Lab_URL_Shortener/Cards/7.md deleted file mode 100644 index 33089bb7..00000000 --- a/Postman_Lab_URL_Shortener/Cards/7.md +++ /dev/null @@ -1,3 +0,0 @@ - - -Additionally, we will need to generate a hash in our address bar with the new shortened URL. Create a function called `genhash()` that will accomplish this. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/8-1-1.md b/Postman_Lab_URL_Shortener/Cards/8-1-1.md index 8a664b0c..4e686ad2 100644 --- a/Postman_Lab_URL_Shortener/Cards/8-1-1.md +++ b/Postman_Lab_URL_Shortener/Cards/8-1-1.md @@ -1,25 +1,76 @@ - + -`sendrequest()` can be defined as follows: +Our `server.js` file should now look like this: ```javascript -function send_request(url) { - this.url = url; - $.ajax({ - 'url': endpoint + "/" + window.location.hash.substr(1), - 'type': 'POST', - 'data': JSON.stringify(this.url), - 'dataType': 'json', - 'contentType': 'application/json; charset=utf-8' +const express = require('express') +const mongoose = require('mongoose') +const ShortUrl = require('./models/shortUrl') +const app = express() + +mongoose.connect('mongodb://localhost/urlShortener', { + useNewUrlParser: true, useUnifiedTopology: true +}) + +app.set('view engine', 'ejs') + +app.use(express.urlencoded({ extended: false })) + +app.get('/', async (req, res) => { + const shortUrls = await ShortUrl.find() + res.render('index', { shortUrls: shortUrls }) }) -} + +app.post('/shortUrls', async (req, res) => { + await ShortUrl.create({ full: req.body.fullUrl }) + + res.redirect('/') +}) + +app.listen(process.env.PORT || 5000); ``` -In this function we send a JSON request to **jsonstore** to store the long URL with a link to short URL. Specifically, we use JQuery to send the JSON request to **endpoint + ”/” + our random string hash from the location bar.** As an example: +...and our `index.ejs` file should be updated to produce each URL in our database: -> ``` -> https://www.jsonstore.io/8ba4fd855086288421f770482e372ccb5a05d906269a34da5884f39eed0418a1/abcd -> ``` -> +```html + + + + + + + + Document + + +
+

URL Shrinker

+
+ + + +
+ + + + + + + + + + + <% shortUrls.forEach(shortUrl => { %> + + + + + + <% }) %> + +
Full URLShort URLClicks
<%= shortUrl.full %><%= shortUrl.short %><%= shortUrl.clicks %>
+
+ + +``` -This way, whenever we send a get request to the above-mentioned URL, we’ll get the long URL as `data`. diff --git a/Postman_Lab_URL_Shortener/Cards/8-1.md b/Postman_Lab_URL_Shortener/Cards/8-1.md deleted file mode 100644 index 6bf951f9..00000000 --- a/Postman_Lab_URL_Shortener/Cards/8-1.md +++ /dev/null @@ -1,3 +0,0 @@ - - -This will require a JSON request, and an explicit use of ajax's capabilities. Don't forget to the add our hash to the endpoint when creating the link. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/8.md b/Postman_Lab_URL_Shortener/Cards/8.md deleted file mode 100644 index 4967894a..00000000 --- a/Postman_Lab_URL_Shortener/Cards/8.md +++ /dev/null @@ -1,5 +0,0 @@ - - -Now we get to the nitty gritty of our program! We've already created a function that will verify the validity of the original URL, and create a new hash for us. Now, we actually need to communicate with our endpoint to store the long URL with a link to the new shortened URL. - -Create a function called `send_request()` to do this. The function will take the long url as input. \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/9-1-1.md b/Postman_Lab_URL_Shortener/Cards/9-1-1.md index 7900c977..8379ce10 100644 --- a/Postman_Lab_URL_Shortener/Cards/9-1-1.md +++ b/Postman_Lab_URL_Shortener/Cards/9-1-1.md @@ -1,14 +1,43 @@ - + -`shorturl()` can be defined as follows: +The final modifications to the `server.js` file will make it be as follows: ```javascript -function shorturl() -{ - var longurl = geturl(); - genhash(); - send_request(longurl); -} +const express = require('express') +const mongoose = require('mongoose') +const ShortUrl = require('./models/shortUrl') +const app = express() + +mongoose.connect('mongodb://localhost/urlShortener', { + useNewUrlParser: true, useUnifiedTopology: true +}) + +app.set('view engine', 'ejs') + +app.use(express.urlencoded({ extended: false })) + +app.get('/', async (req, res) => { + const shortUrls = await ShortUrl.find() + res.render('index', { shortUrls: shortUrls }) +}) + +app.post('/shortUrls', async (req, res) => { + await ShortUrl.create({ full: req.body.fullUrl }) + + res.redirect('/') +}) + +app.get('/:shortUrl', async (req, res) => { + const shortUrl = await ShortUrl.findOne({ short: req.params.shortUrl }) + if (shortUrl == null) return res.sendStatus(404) + + shortUrl.clicks++ + shortUrl.save() + + res.redirect(shortUrl.full) +}) + +app.listen(process.env.PORT || 5000); ``` -Very straightforward! \ No newline at end of file +This ensures that when you click on the short URL, you get redirected to the webpage referred to by the full URL. With this, we are done! \ No newline at end of file diff --git a/Postman_Lab_URL_Shortener/Cards/9.md b/Postman_Lab_URL_Shortener/Cards/9.md deleted file mode 100644 index a78dff07..00000000 --- a/Postman_Lab_URL_Shortener/Cards/9.md +++ /dev/null @@ -1,5 +0,0 @@ - - -Remember the `shorturl()` function you mentioned in `index.html`? It's time to fully implement it. - -Create a `shorturl()` function that will call all of the previously defined functions and execute them. \ No newline at end of file