-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (72 loc) · 2.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const express=require('express')
const shortid = require('shortid') // for generating short-urls
const connectDB=require('./config/db') // for connection to the DB
const ShortUrl=require('./models/shortUrl') // for Schema
// connecting to database
connectDB()
const app=express()
const host=process.env.host || "localhost"
const port=process.env.PORT || 5000
app.set('view engine', 'ejs')
app.use(express.static("public"));
app.set('views', 'views');
app.use(express.urlencoded({
extended: false
}))
app.get('/', (req, res)=>{
res.render('index')
})
const checkIfAlreadyPresent=async (shortUrlGenerated)=>{
const likeUrl2=await ShortUrl.findOne({short: shortUrlGenerated})
if(likeUrl2==null) // not found in db
return false
else
return true
}
const generateShortURL=()=>{
return shortid.generate()
}
// handling a url-shortening request
app.post('/shortURL', async(req, res)=>{
let shortUrlGenerated=generateShortURL()
/*
check if the 'full' and 'short' url already exist in the db,
that is the relation should be ONE-ONE
in other words, one 'full' url should map to one 'short' url
*/
// if the generated url (shortUrlGenerated) matches in the db, then regenerate
while(checkIfAlreadyPresent(shortUrlGenerated)===true){
console.log("Short URL already exists, regenerate.")
shortUrlGenerated=generateShortURL()
}
console.log("Short URL generated.")
// create a new URL in the db
await ShortUrl.create({
full: req.body.fullUrl,
short: shortUrlGenerated
})
console.log("Doc inserted");
// this url is an Object {
// short:
// full:
// clicks:
// }
const url=await ShortUrl.findOne({full: req.body.fullUrl, short: shortUrlGenerated})
res.render('showURL', {url: url})
})
// ?handling the 'get' request of 'short' URL
// and redirecting it to 'full' URL
app.get('/:shortUrl', async(req, res)=>{
console.log("Short url got: ", req.params.shortUrl);
console.log("type of shortURL: ", typeof req.params.shortUrl);
const url=await ShortUrl.findOne({
short: req.params.shortUrl
})
console.log("URL: ", url);
// shortened version -> url.short
// full version -> url.full
res.redirect(url.full)
})
app.listen(port, ()=>{
console.log(`Listening on http://${host}:${port}`)
})