-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (35 loc) · 1.2 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
48
49
50
51
52
53
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const urlRouter = require("./routes/url");
const homeRouter = require("./routes/home");
const redirectRouter = require("./routes/redicect");
const userRouter = require("./routes/user");
const analyticRouter = require("./routes/analytics");
const { connectToMongoDB } = require("./connections");
const { restrictToLoggedInUserOnly, cheackAuth } = require("./middlewares/auth");
// Server creation
const app = express();
const PORT = 8000;
// views enginge
app.set("view engine", "ejs");
app.set('views', path.resolve("./views"));
// middlewares
app.use(express.json());
app.use(express.urlencoded({extended:false}));
app.use(cookieParser());
// connect to database
const dbUrl = "mongodb://127.0.0.1:27017/";
connectToMongoDB(`${dbUrl}urlshort`).then(()=>{
console.log("Mongodb Connected!");
});
// routes
app.use("/url",restrictToLoggedInUserOnly, urlRouter);
app.use("/redirect", redirectRouter);
app.use("/analytics", cheackAuth, analyticRouter);
app.use("/user", userRouter);
app.use("/", homeRouter);
// Server Listen
app.listen(PORT,()=>{
console.log(`Server Started at ${PORT}..`);
});