-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex.js
37 lines (30 loc) · 1.22 KB
/
Index.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 useUrlRouter = require('./routes/url')
const mongoose = require('mongoose')
const path = require('path')
const signRouter = require('./routes/user')
const cookieParser = require('cookie-parser')
const { restrictLoggedInUserOnly } = require('./middlewares/restrict')
const URL = require("./models/url")
mongoose.connect('mongodb://localhost:27017/urlShortner')
.then((r)=>console.log('mongoDB connected!'))
.catch((e)=>console.log('not connected'))
const app = express()
const PORT = 8001
// app.use(restrictLoggedInUserOnly()) ye yaha nhi ayega kuki req res hai toh ye 'url wle m jyga n
app.use(cookieParser())
app.use(express.urlencoded({ extended: false }));
app.set('view engine', 'ejs');//bta do konsa template engine use kr rhe ho
app.set('views', path.resolve('./views'));//kaha pr h
app.use('/url',restrictLoggedInUserOnly,useUrlRouter);
app.use('/user',signRouter);
app.get('/show/:shortId',async (req,res)=>{
const mainUrl = await URL.findOneAndUpdate(
{ shortID: req.params.shortId },
{
$push: { visitHistory: Date.now() },
}
);
res.redirect(mainUrl.redirectUrl);
})
app.listen(PORT,()=>console.log(`server started at port : ${PORT}`));