-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
111 lines (95 loc) · 3.17 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
if (process.env.NODE_ENV !== "production") {
require("dotenv").config({ path: "./config.env" })
}
const createError = require("http-errors")
const express = require("express")
const path = require("path")
const cookieParser = require("cookie-parser")
const logger = require("morgan")
const exphbs = require("express-handlebars")
const mongoose = require("mongoose")
const handlebars = require("handlebars")
const passport = require("passport")
const flash = require("express-flash")
const session = require("express-session")
const methodOverride = require("method-override")
const { allowInsecurePrototypeAccess } = require("@handlebars/allow-prototype-access")
const { findUserByEmail, findUserById } = require("./controllers/usersController")
const usersRoutes = require("./routes/usersRoutes")
const questionsRoutes = require("./routes/questionsRoutes")
const answersRoutes = require("./routes/answersRoutes")
const topicsRoutes = require("./routes/topicsRoutes")
const app = express()
//database setup
mongoose.connect(process.env.DATABASE_LOCAL, { useNewUrlParser: true, useUnifiedTopology: true })
const db = mongoose.connection
db.on("error", (error) => console.error(error))
db.once("open", () => console.log("Connected to Database"))
// view engine setup
app.engine(
".hbs",
exphbs({
helpers: {
//this custom helper will help me see data that i pass from express to a .hbs view in the js script file
json: (context) => JSON.stringify(context),
formatDate: (date) => new Date(parseInt(date)).toDateString(),
capitalize: (str) =>
str
? str
.split(" ")
.map((s) => s[0].toUpperCase() + s.slice(1))
.join(" ")
: "",
upperFirstChar: (str) => (str ? str[0].toUpperCase() + str.slice(1) : "")
},
extname: ".hbs",
handlebars: allowInsecurePrototypeAccess(handlebars)
})
)
app.set("view engine", ".hbs")
//passport setup
const initializePassport = require("./utils/passport-config")
initializePassport(passport, findUserByEmail, findUserById)
//middlwares
if (process.env.NODE_ENV !== "production") {
app.use(logger("dev"))
}
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(flash())
app.use(
session({
secret: "secret",
resave: false,
saveUninitialized: false
})
)
app.use(express.static(path.join(__dirname, "public")))
app.use(passport.initialize())
app.use(passport.session())
app.use(methodOverride("_method"))
// routes
app.use("/users", usersRoutes)
app.use("/questions", questionsRoutes)
app.use("/answers", answersRoutes)
app.use("/topics", topicsRoutes)
app.get("/", (req, res, next) => {
res.redirect("/topics")
})
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404))
})
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get("env") === "development" ? err : {}
// render the error page
res.status(err.status || 500)
res.render("error")
})
// module.exports = app
const PORT = process.env.PORT || 3000
app.listen(PORT, () => console.log(`app started in mode ${process.env.NODE_ENV},it is listening on port ${PORT}`))