-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
47 lines (37 loc) · 1.38 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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const keys = require('./config/keys');
const ShortUrl = require('./models/shortUrl');
const Clicks = require('./models/clicks');
const app = express();
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: false }));
mongoose.connect(keys.mongodb.dbURI,{ useUnifiedTopology: true, useNewUrlParser: true }, () => {
console.log('connected to mongodb');
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', async (req, res) => {
const shortUrls = await ShortUrl.find();
res.render('index', {shortUrls: shortUrls});
});
app.post('/urlshortener', async (req, res) => {
await ShortUrl.create({fullURL: req.body.fullURL});
res.redirect('/');
});
app.get('/:shortUrl', async (req, res) => {
const shortUrl = await ShortUrl.findOne({shortURL: req.params.shortUrl});
if(shortUrl == null) return res.sendStatus('404');
shortUrl.clicks++;
shortUrl.save();
await Clicks.create({shortURL: shortUrl.shortURL});
res.redirect(shortUrl.fullURL);
});
app.get('/display/graph/:shortUrl', async (req, res) => {
const clicks = await Clicks.find({shortURL: req.params.shortUrl});
res.render('clicks', {clicks: clicks})
});
app.listen(3000, () => {
console.log("app listening on port 3000");
});