-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (61 loc) · 1.75 KB
/
app.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const express = require("express")
const expressLayouts = require("express-ejs-layouts")
const dotenv = require("dotenv").config()
const session = require("express-session")
const cookieParser = require("cookie-parser")
const bodyParser = require("body-parser")
const methodOverride = require("method-override")
const { flash } = require("express-flash-message")
const Swal = require("sweetalert2")
const morgan = require("morgan")
const jwt = require("jsonwebtoken")
//routers
const userRouter = require("./server/routes/user")
const adminRouter = require("./server/routes/admin")
const app = express()
const port = process.env.PORT || 3000
//Session & cookies
app.use(cookieParser())
app.use(
session({
secret: "some secret",
cookie: {
maxAge: 1000 * 60 * 60 * 24, // 24 hours
},
resave: false,
saveUninitialized: false,
})
)
//morgan
// app.use(morgan("dev"))
// setup flash
app.use(flash({ sessionKeyName: "express-flash-message" }))
//Template engine
app.use(expressLayouts)
app.set("layout", "./layouts/authLayout", "./layouts/adminLayout", "./layouts/userLayout")
app.set("view engine", "ejs")
//bodyparser
app.use(
bodyParser.urlencoded({
extended: true,
})
)
//Database connection
const connectDB = require("./server/config/mongodb")
connectDB()
//serving public file
app.use(express.static(__dirname + "/public"))
//methodOvveride
app.use(methodOverride("_method"))
// parsing the incoming data
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
//Routes
app.use("/", userRouter)
app.use("/admin", adminRouter)
//404
app.get("*", (req, res) => {
res.status(404).render("404")
})
//start server
app.listen(port, () => console.log(`Server started on port ${port}`))