├── .gitignore ├── .idea ├── misc.xml ├── jsLibraryMappings.xml └── vcs.xml ├── static ├── script.js ├── index.html └── style.css ├── package.json ├── LICENSE ├── server.js ├── README.md └── shortner.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | package-lock.json 3 | .env 4 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/jsLibraryMappings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /static/script.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by rishabhshukla on 09/03/17. 3 | */ 4 | $(function () { 5 | $('#submit').click(function () { 6 | var url = $('#url').val(); 7 | $.post('/api/v1/shorten', { 8 | url:url 9 | },function (data) { 10 | $('#shortcode').html("Short URL: " + ''+window.location.href+data + ''); 11 | }) 12 | }) 13 | }) -------------------------------------------------------------------------------- /static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | URL Shortner 10 | 11 | 12 | 13 |
14 |

URL Shortener

15 |
16 | 17 |
18 | 19 |
20 | 21 | 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "urlshortener", 3 | "version": "1.0.0", 4 | "main": "server.js", 5 | "dependencies": { 6 | "body-parser": "^1.17.1", 7 | "convert-radix64": "^0.2.0", 8 | "dotenv": "^16.4.7", 9 | "express": "^4.15.2", 10 | "firebase": "^3.7.0", 11 | "hasha": "^2.2.0" 12 | }, 13 | "scripts": { 14 | "test": "echo \"Error: no test specified\" && exit 1", 15 | "start": "node server.js" 16 | }, 17 | "author": "", 18 | "license": "ISC", 19 | "repository": { 20 | "type": "git", 21 | "url": "git+https://github.com/rishz/URL-Shortener.git" 22 | }, 23 | "bugs": { 24 | "url": "https://github.com/rishz/URL-Shortener/issues" 25 | }, 26 | "homepage": "https://github.com/rishz/URL-Shortener#readme", 27 | "description": "" 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Rishabh Shukla 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by dragon0513 on 09/03/21. 3 | */ 4 | 5 | 6 | const express = require("express"); 7 | const bodyParser = require("body-parser"); 8 | const app = express(); 9 | const shortner = require("./shortner"); 10 | const port = 4100; 11 | 12 | app.use(bodyParser.json()); 13 | app.use(bodyParser.urlencoded({extended:true})) 14 | app.use(express.static("static")); 15 | 16 | app.get('/:shortcode',(req,res) => { 17 | 18 | shortner.expand(req.params.shortcode) 19 | .then((url) => { 20 | res.redirect(url); 21 | }) 22 | .catch((error) => { 23 | 24 | }); 25 | }); 26 | 27 | app.post('/api/v1/shorten', (req, res) => { 28 | 29 | let url = req.body.url; 30 | let shortcode = shortner.shorten(url); 31 | res.send(shortcode); 32 | 33 | }); 34 | 35 | app.get('/api/v1/expand/:shortcode', (req, res) => { 36 | 37 | let shortcode = req.body.shortcode; 38 | let url = url.expand(shortcode); 39 | res.send(url); 40 | 41 | }); 42 | 43 | app.listen(port, () => { 44 | 45 | console.log("Listening on port "+port); 46 | 47 | }); 48 | 49 | //console.log(shortner.shorten('http://google.com')); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # URL-Shortener 2 | 3 | license 4 | 5 | A URL-Shortener created using Node-JS and synced with Firebase Database. 6 | 7 | 8 | ## Demo 9 | ![out](https://cloud.githubusercontent.com/assets/20211622/23782082/be677c3e-0577-11e7-9851-4ceb3fda7367.gif) 10 | 11 | ## Why use it? 12 | 13 | Great tool for shortening a long URL to just 4 characters. 14 | 15 | ## Features 16 | 17 | * Shorten a Long URL using the web app. 18 | * Gets saved in Firebase database for quick access 19 | 20 | ## Built With 21 | 22 | * node js (express js) 23 | * firebase 24 | * javascript 25 | * html 26 | * css 27 | * jquery 28 | 29 | ## Screenshot 30 | 31 | ![screens](https://cloud.githubusercontent.com/assets/20211622/23782143/106745a0-0578-11e7-96fb-eda680be53f4.png) ![screen shot 2017-03-10 at 10 16 40 am](https://cloud.githubusercontent.com/assets/20211622/23782539/325a4e16-057b-11e7-951a-641b63e1022d.png) 32 | 33 | 34 | Contributing 35 | ========== 36 | Any kind of contributions are welcome. 37 | 38 | 1. **Fork** the repo on GitHub. 39 | 2. **Clone** the project to your own machine. 40 | 3. **Commit** changes to **development** branch. 41 | 4. **Push** your work back up to your fork. 42 | 5. Submit a **Pull request** so that I can review your changes 43 | -------------------------------------------------------------------------------- /static/style.css: -------------------------------------------------------------------------------- 1 | *{ 2 | align: center; 3 | } 4 | #url{ 5 | align: center; 6 | size: 100px; 7 | height: 50px; 8 | width: 600px; 9 | font-family: Arial; 10 | font-size: 25px; 11 | margin-top: 2%; 12 | margin-left: 25%; 13 | } 14 | .btn { 15 | background: #ffd324; 16 | background-image: -webkit-linear-gradient(top, #ffd324, #fab120); 17 | background-image: -moz-linear-gradient(top, #ffd324, #fab120); 18 | background-image: -ms-linear-gradient(top, #ffd324, #fab120); 19 | background-image: -o-linear-gradient(top, #ffd324, #fab120); 20 | background-image: linear-gradient(to bottom, #ffd324, #fab120); 21 | border-radius: 15px; 22 | font-family: sans-serif; 23 | color: #ffffff; 24 | font-size: 29px; 25 | padding: 10px 20px 10px 20px; 26 | text-decoration: black; 27 | margin-top: 2%; 28 | margin-left: 43%; 29 | } 30 | 31 | .btn:hover { 32 | background: #3cb0fd; 33 | background-image: -webkit-linear-gradient(top, #3cb0fd, #3498db); 34 | background-image: -moz-linear-gradient(top, #3cb0fd, #3498db); 35 | background-image: -ms-linear-gradient(top, #3cb0fd, #3498db); 36 | background-image: -o-linear-gradient(top, #3cb0fd, #3498db); 37 | background-image: linear-gradient(to bottom, #3cb0fd, #3498db); 38 | text-decoration: none; 39 | } 40 | 41 | .mydiv{ 42 | align-self: center; 43 | align-items: center; 44 | font-family: "American Typewriter"; 45 | font-size:30px; 46 | font-style: inherit; 47 | margin-top: 3%; 48 | margin-left: 25%; 49 | 50 | } 51 | .center{ 52 | font-family: "American Typewriter"; 53 | font-size: 40px; 54 | } -------------------------------------------------------------------------------- /shortner.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by dragon0513 on 09/03/21. 3 | */ 4 | var firebase = require('firebase'); 5 | const r = require('convert-radix64'); 6 | const hasha = require("hasha"); 7 | const dotenv = require('dotenv'); 8 | dotenv.config(); 9 | const hashMap = {}; 10 | 11 | var config = { 12 | apiKey: process.env.API_KEY, 13 | authDomain: process.env.AUTH_DOMAIN, 14 | databaseURL: process.env.DATABASE_URL, 15 | storageBucket: process.env.STORAGE_BUCKET, 16 | }; 17 | firebase.initializeApp(config); 18 | 19 | module.exports = { 20 | shorten: (url) => { 21 | hash = hasha(url, {encoding:"base64", algorithm:"md5"}); 22 | hash = hash.slice(0,4); 23 | 24 | hash = hash.replace('/','-'); 25 | hash = hash.replace('+','_'); 26 | // let hashInt = parseInt(hash,16) 27 | // conv = atob(hashInt); 28 | hashMap[hash] = url; 29 | writeUserData(url,r.from64(hash),hash); 30 | 31 | return hash; 32 | 33 | }, 34 | expand: (shortcode) => { 35 | 36 | return new Promise(function(resolve, reject){ 37 | 38 | if(shortcode === undefined){ 39 | reject(null); 40 | } 41 | var ref = firebase.database().ref('/'+r.from64(shortcode)); 42 | 43 | ref.once('value').then(function(snapshot) { 44 | val = snapshot.val(); 45 | if(val){ 46 | let url = val.url; 47 | resolve(url); 48 | }else{ 49 | resolve(hashMap[shortcode]); 50 | } 51 | }); 52 | 53 | }); 54 | } 55 | }; 56 | 57 | writeUserData = (url,shortcode,code) => { 58 | firebase.database().ref('/'+shortcode).set({ 59 | code:code, 60 | url:url 61 | }); 62 | } 63 | --------------------------------------------------------------------------------