-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (60 loc) · 2.51 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const express = require("express")
const mongoose = require("mongoose")
const expressSession = require("express-session")
const flash = require("connect-flash")
const ejs = require("ejs")
const bodyParser = require("body-parser")
const fileUpload = require("express-fileupload")
const homeController = require("./controllers/home")
const contactController = require("./controllers/contact")
const aboutController = require('./controllers/about')
const newPostController = require("./controllers/newPost")
const getPostController = require("./controllers/getPost")
const storePostController = require("./controllers/storePost")
const registerController = require("./controllers/register")
const storeUserController = require("./controllers/storeUser")
const loginController = require("./controllers/login")
const loginUserController = require("./controllers/loginUser")
const logoutController = require("./controllers/logout")
const validationMiddleware = require("./middleware/validationMiddleware")
const authMiddleware = require("./middleware/authMiddleware")
const redirectIfAuth = require("./middleware/redirectIfAuthenticatedMiddleware")
const config = process.env.PRODUCTION ? process.env : require("./config")
mongoose.connect(config.connection_string, {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true,
})
const app = new express()
app.use(expressSession({
secret: config.session_secret
}))
global.loggedIn = null
app.use("*", (req, res, next) => {
loggedIn = req.session.userId
next()
})
app.set("view engine", "ejs")
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(fileUpload())
app.use(express.static("public"))
app.use(flash())
let port = process.env.PRODUCTION ? process.env.PORT : config.port
app.listen(port, ()=>{
console.log(`App listening on port ${port}`)
})
app.get("/", homeController)
app.get("/about", aboutController)
app.get("/post/:id", getPostController)
app.get("/contact", contactController)
app.get("/posts/new", authMiddleware, newPostController)
app.use("/posts/store", validationMiddleware)
app.post("/posts/store", authMiddleware, storePostController)
app.get("/auth/register", redirectIfAuth, registerController)
app.post("/users/register", redirectIfAuth, storeUserController)
app.get("/auth/login", redirectIfAuth, loginController)
app.post("/users/login", redirectIfAuth, loginUserController)
app.get("/auth/logout", authMiddleware, logoutController)
app.use((req, res) => res.render("notfound"))