-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (32 loc) · 1.02 KB
/
index.js
File metadata and controls
62 lines (32 loc) · 1.02 KB
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
require('dotenv').config()
var express = require('express');
const mongoose = require('mongoose')
var app = express();
let url = 'mongodb+srv://sheheer:sheheer@cluster0.optng.mongodb.net/ShoppingCartDB'
mongoose.connect(process.env.dbUrl || url , {
useNewUrlParser: true,
useUnifiedTopology: true
})
const Shorturl = require('./models/urlShort')
app.set('view engine', 'ejs')
app.use(express.urlencoded({ extended: false }))
app.get('/', async (req, res) => {
const shortUrls = await Shorturl.find()
res.render('index', { shortUrls: shortUrls })
});
app.post('/shortUrl', async (req, res) => {
await Shorturl.create({
full: req.body.fullUrl,
short: req.body.shortUrl
})
res.redirect('/')
})
app.get('/:shorturl',async (req,res)=>{
const url = await Shorturl.findOne({short: req.params.shorturl})
console.log(url);
if(url == null) return res.sendStatus(404)
url.clicks++;
url.save();
res.redirect(url.full)
})
app.listen(process.env.PORT || 8000);