├── README.md ├── models └── databaseSchema.js ├── package.json ├── server.js └── views └── index.ejs /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ksdkamesh99/shortenurl.ml/main/README.md -------------------------------------------------------------------------------- /models/databaseSchema.js: -------------------------------------------------------------------------------- 1 | const mongoose=require('mongoose'); 2 | 3 | const shortid= require('shortid'); 4 | 5 | const databaseSchema=new mongoose.Schema({ 6 | long:{ 7 | type: String, 8 | required:true, 9 | trim:true 10 | }, 11 | short: { 12 | type: String, 13 | default: shortid.generate 14 | 15 | }, 16 | clicks: { 17 | type:Number, 18 | default:0 19 | } 20 | }); 21 | 22 | const shortenUrl=mongoose.model('shortenUrl',databaseSchema); 23 | 24 | module.exports=shortenUrl; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "url-shortner", 3 | "version": "1.0.0", 4 | "description": "��#\u0000 \u0000S\u0000t\u0000u\u0000r\u0000d\u0000y\u0000U\u0000R\u0000L\u0000-\u0000S\u0000h\u0000o\u0000r\u0000t\u0000n\u0000e\u0000r\u0000\r\u0000 \u0000", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/ksdkamesh99/SturdyURL-Shortner.git" 12 | }, 13 | "author": "", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/ksdkamesh99/SturdyURL-Shortner/issues" 17 | }, 18 | "homepage": "https://github.com/ksdkamesh99/SturdyURL-Shortner#readme", 19 | "dependencies": { 20 | "ejs": "^3.1.5", 21 | "express": "^4.17.1", 22 | "mongoose": "^5.11.8", 23 | "shortid": "^2.2.16" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express=require('express'); 2 | const app=express(); 3 | const mongoose=require('mongoose'); 4 | const shortenUrl = require('./models/databaseSchema'); 5 | 6 | app.use(express.urlencoded({extended: false})); 7 | 8 | let urllg; 9 | 10 | mongoose.connect('mongodb+srv://user:tngzF5jkyoobC66d@cluster0.4qtzn.mongodb.net/test?retryWrites=true&w=majority', { 11 | useNewUrlParser: true, 12 | useUnifiedTopology:true 13 | }); 14 | 15 | 16 | 17 | 18 | app.set('view engine','ejs'); 19 | 20 | app.get('/',async (req,res)=>{ 21 | const result= await shortenUrl.find(); 22 | res.render('index',{result:result}); 23 | }); 24 | 25 | 26 | 27 | app.post('/shortenurl',async (req,res)=>{ 28 | const url=req.body.long; 29 | await shortenUrl.create({long:req.body.long}); 30 | res.redirect('/'); 31 | 32 | }); 33 | 34 | 35 | app.get('/:shorturl',async (req,res)=>{ 36 | const urlsh=req.params.shorturl; 37 | myresult=await shortenUrl.findOne({short:urlsh}); 38 | if(myresult==null){ 39 | res.sendStatus(404); 40 | } 41 | else{ 42 | urllg=myresult.long; 43 | myresult.clicks++; 44 | myresult.save(); 45 | 46 | } 47 | res.redirect(urllg); 48 | 49 | }); 50 | 51 | 52 | 53 | 54 | app.listen(5000,'0.0.0.0'); -------------------------------------------------------------------------------- /views/index.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 || Long URL | 40 |Short URL | 41 |No of Clicks | 42 |
|---|---|---|
| <%= shurl.long %> | 48 |<%= shurl.short %> | 49 |<%= shurl.clicks %> | 50 |