-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (48 loc) · 1.72 KB
/
server.js
File metadata and controls
60 lines (48 loc) · 1.72 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
57
58
59
60
const path = require('path')
const express = require('express');
const connectDB = require('./config/dbConfig')
require('dotenv').config();
const shortUrlModel = require('./models/shortUrl')
const shortid = require('shortid');
const app = express();
app.use(express.urlencoded({ extended: false }))
app.use(express.static(path.join(__dirname, 'public')))
app.set('view engine', 'ejs');
app.get('/', async (req, res) => {
res.render('index', {urlFound: null})
})
app.post('/short-urls', async (req, res) => {
const personalDomain = req.body.domain;
const shortUrl = shortid.generate();
const newUrl = {
fullUrl: req.body.fullUrl,
shortUrl: personalDomain ? personalDomain : shortUrl
}
await shortUrlModel.create(newUrl);
// res.redirect('/');
res.render('short', {newUrl})
})
app.post('/short-details', async (req, res) => {
const urlFound = await shortUrlModel.findOne({shortUrl: req.body.checkShort})
if (!urlFound) return res.render('details', {urlFound: null, message: `The url ${req.body.checkShort} can't be found!`})
res.render('details', {urlFound: urlFound, message: null})
})
app.get('/:shorturl', async (req, res) => {
const url = await shortUrlModel.findOne({ shortUrl: req.params.shorturl });
if (!url) return res.status(404).send(`The short url ${req.params.shorturl} does not exist`);
url.clicks++;
url.save();
res.redirect(url.fullUrl)
})
//connect database and listen to port
const connectAndListen = async () => {
try {
await connectDB();
app.listen(3070, function() {
console.log(`Listening on port 3070`);
});
} catch (error) {
console.log(error.message);
}
}
connectAndListen();