-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
37 lines (27 loc) · 922 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
33
34
35
36
37
const express = require("express");
const app = express();
const shortUrl = require("./models/shortUrl");
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/urlShortener", {});
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("/shortUrls", async (req, res) => {
// res.render('index')
console.log(req.body)
await shortUrl.create({ full: req.body.fullUrl });
res.redirect("/");
});
app.get('/:shortUrl',async (req,res)=>{
const code = await shortUrl.findOne({short: req.params.shortUrl})
if(!code)
return res.status(404).sendStatus(404)
console.log(code)
code.clicks++;
code.save()
res.redirect(code.full)
})
app.listen(process.env.PORT || 3000);