-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
39 lines (29 loc) · 960 Bytes
/
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
const express = require('express')
const mongoose = require('mongoose')
const ShortURL = require('./models/shorturl.model')
const app = express()
mongoose.connect('mongodb://localhost/shorty_db', {
useNewUrlParser: true,
useUnifiedTopology: true
})
// Sets the View Engine to EJS
app.set('view engine', 'ejs')
app.use(express.urlencoded({ extended: false }))
// URL Routes
app.get('/', async (req, res) => {
const allShortUrls = await ShortURL.find()
res.render('index', {allShortUrls: allShortUrls})
})
app.post('/chopped', async (req, res) => {
await ShortURL.create({ fullURL: req.body.fullURL})
res.redirect('/')
})
app.get('/:shortVal', async(req, res) => {
const shortVal = await ShortURL.findOne({shortURL: req.params.shortVal})
if(shortVal == null) return res.sendStatus(404)
shortVal.clickURL++
shortVal.save()
res.redirect(shortVal.fullURL)
})
// ENV Ports
app.listen(process.env.PORT || 8000);