-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (38 loc) · 1.23 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
38
39
40
41
42
43
44
45
46
47
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser")
const { connectToMongoDB } = require("./connect")
const { restrictToLoggedinUserOnly, checkAuth } = require("./middlewares/auth")
const URL = require("./models/url")
const urlRoute = require('./routes/url')
const staticRoute = require("./routes/staticRouter")
const userRoute = require("./routes/user")
const app = express();
const PORT = 5001;
connectToMongoDB("mongodb://127.0.0.1:27017/short-url")
.then(()=> console.log("mongoDB connected"));
app.set("view engine", "ejs");
app.set("views", path.resolve("./views"))
app.use(express.json());
app.use(express.urlencoded({ extended: false}));
app.use(cookieParser())
app.use("/url", restrictToLoggedinUserOnly, urlRoute);
app.use("/", checkAuth, staticRoute);
app.use("/user", userRoute)
app.get("/:shortId", async (req,res) => {
const shortId = req.params?.shortId;
if(shortId){
const entry = await URL.findOneAndUpdate(
{shortId:shortId},
{
$push: {
visitHistory: {
timestamp: Date.now(),
},
},
},
);
res.redirect(entry.redirectUrl);
}
});
app.listen(PORT, ()=> console.log("Server started.."))