-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (37 loc) · 1.22 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const path = require('path');
const ShortUrl = require('./models/sortURL')
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const PORT = 5000 || process.env.PORT;
const db = require('./config/keys').mongoURI;
mongoose.connect(db,{ useNewUrlParser: true,useUnifiedTopology: true })
.then(() => console.log('MongoDB connected....'))
.catch(err => console.log(err));
app.get("/",(req,res) => {
res.sendFile(path.join(__dirname,'./index.html'));
})
app.get("/short", async (req,res) => {
var obj = await ShortUrl.find();
res.send(obj);
})
app.post("/shorten",async (req,res) => {
await ShortUrl.create({fullUrl : req.body.short})
// console.log('Hello');
res.redirect('/')
})
app.get("/:shortt", async (req,res) => {
// console.log(req.params.shortt);
let url = await ShortUrl.findOne({shortUrl : req.params.shortt});
if(url==null)
{
res.sendStatus(404);
}
url.clickedNo++;
url.save();
res.redirect(url.fullUrl);
})
app.listen(PORT, () => console.log(`Server Started on port : ${PORT}`));