-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
45 lines (36 loc) · 1.21 KB
/
server.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
const express = require('express')
// const resp = require('express/lib/response')
const mongoose = require('mongoose')
const ShortUrl = require('./models/shortUrl')
const app = express()
require('dotenv').config()
const PORT = process.env.PORT || 1111
const db = async ()=>{
try{
const conn = await mongoose.connect(process.env.MONGODB_URI,{ useNewUrlParser: true, useUnifiedTopology: true})
console.log(`Database Connected: ${conn.connection.host}`)
} catch (error){
console.log(error)
process.exit(1)
}
}
app.set('view engine', 'ejs')
app.use(express.urlencoded({ extended: false }))
app.get('/', async (req, resp) => {
const shortUrls = await ShortUrl.find()
resp.render('index', { shortUrls: shortUrls })
})
app.post('/shortUrls', async (req, resp) => {
await ShortUrl.create({ full: req.body.fullUrl })
resp.redirect('/')
})
app.get('/:shortUrl', async (req, resp) => {
const shortUrl = await ShortUrl.findOne({ short: req.params.shortUrl })
if (shortUrl == null) return resp.sendStatus(404)
shortUrl.clicks++
shortUrl.save()
resp.redirect(shortUrl.full)
})
db().then(()=>{
app.listen(PORT,()=> console.log(`Listening on port ${PORT}`))
})