-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (35 loc) · 1.14 KB
/
app.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
const express = require('express')
const mongoose = require('mongoose')
const shortUrl = require('./models/shorlUrl')
var dotenv = require('dotenv')
dotenv.config();
const app = express()
//connecting to mongodb
const dburl = process.env.URI;
mongoose.connect(dburl, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('DB connected successfully...'))
.catch((err) => console.log(err));
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.set('view engine','ejs')
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})
res.redirect('/')
})
app.get('/login', (req,res)=>{
res.render('login.ejs')
})
app.get('/:shorturl',async(req,res)=>{
const Url = await shortUrl.findOne({short: req.params.shorturl})
if(Url == null){
res.send('<h1>Error 404! Page not Found</h1>')
}
Url.click++;
Url.save();
res.redirect(Url.full)
})
app.listen(80,console.log(`The server is running at http://127.0.0.1:80`));