-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
68 lines (59 loc) · 2.18 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
require('dotenv').config()
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
mongoose.connect(process.env.DBURI, { useNewUrlParser: true })
const Url = mongoose.model('Url', {
url: String,
shorturl: String,
iscustom: Boolean,
clicks: { type: Number, default: 0 },
date: {
type: Date,
default: Date.now
}
})
app.set('view engine', 'ejs')
app.use(express.static('web/dist'))
app.use(bodyParser.json())
app.post('/short', async (req, res) => {
let shorturl = false
let iscustom = false
if (!req.body.url) return res.json({ sucess: false, err: 'No url provided' })
if (!/http:\/\/|https:\/\//g.test(req.body.url)) return res.json({ sucess: false, err: 'Invalid url' })
if (req.body.custom) {
if (/[^a-zA-Z0-9]/g.test(req.body.custom)) return res.json({ sucess: false, err: 'Invalid custom url' })
let existing = await Url.findOne({ shorturl: req.body.custom })
if (existing) return res.json({ sucess: false, err: 'Custom url already taken' })
shorturl = req.body.custom
iscustom = true
} else {
shorturl = await generateURL()
}
const data = { url: req.body.url, shorturl, iscustom }
new Url(data).save().then(() => { res.json({ sucess: true, data }) }).catch(() => { res.json({ sucess: false, err: 'Error saving to database.' }) })
})
function generateURL () {
return new Promise(async (resolve, reject) => {
let shorturl = Math.random().toString(36).substring(7)
let existing = await Url.findOne({ shorturl })
resolve(existing ? await generateURL() : shorturl)
})
}
app.post('/status', async (req, res) => {
if (req.body.list) {
let list = req.body.list
let result = await Promise.all(list.map(async item => {
let dbentry = await Url.findOne({ shorturl: item.short })
item['clicks'] = dbentry ? dbentry.clicks : 0
return item
}))
res.json(result)
}
})
app.get('/:shorturl', async (req, res) => {
let url = await Url.findOneAndUpdate({ shorturl: req.params.shorturl }, { $inc: { clicks: 1 } })
res.redirect(url ? url.url : '/')
})
app.listen(process.env.PORT, () => { console.log(`Listening on port ${process.env.PORT}`) })