-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
32 lines (23 loc) · 1023 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
const express = require('express')
const mongoose = require('mongoose')
const app = express()
const ShortUrl = require('./models/shortUrl') //ShortUrl is name of model and const now
app.use(express.urlencoded({extended:false}))
mongoose.connect('mongodb://localhost/UrlShrinker',{
useNewUrlParser:true,useUnifiedTopology:true
})
app.set('view engine', 'ejs')
app.get('/', async (req,res)=>{
const shortUrls = await ShortUrl.find() //shortUrls just consists of the short urls in model and displays on the page
res.render('index',{shortUrls:shortUrls})
})
app.post('/shortUrls', async(req,res)=>{ //same as form action
await ShortUrl.create({full: req.body.fullUrl}) // ShortUrl (the one defined above)
res.redirect('/')
})
app.get('/:shortUrl', async (req,res)=>{
const shortUrl = await ShortUrl.findOne({short:req.params.shortUrl}) // ShortUrl model shortUrl is the /xys
if(shortUrl==null) return res.sendStatus(404)
res.redirect(shortUrl.full)
})
app.listen(process.env.PORT || 3000)