-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (102 loc) · 3.3 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
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
const express = require("express");
const ejs = require("ejs");
const path = require("path");
const mongoose = require("mongoose");
const flash = require("connect-flash");
const fileUpload = require("express-fileupload");
const newPostController = require("./controllers/newPost");
const aboutContorller = require("./controllers/about");
const contactController = require("./controllers/contact");
const homeController = require("./controllers/home");
const storePostController = require("./controllers/storePost");
const getPostController = require("./controllers/getPost");
const validateMiddleware = require("./middlewares/validationMiddleware");
const newUserController = require("./controllers/newUser");
const storeUserController = require("./controllers/storeUser");
const loginController = require("./controllers/login");
const loginUserController = require("./controllers/loginUser");
const expressSession = require("express-session");
const authMiddleware = require("./middlewares/authMiddleware");
const redirectIfAuthenticatedMiddleware = require("./middlewares/redirectIfAuthenticatedMiddleware");
const logoutController = require("./controllers/logout");
var geolang = require("geolang-express");
var i18n = require("i18n-express");
global.loggedIn = null;
// const dbConUrl = "mongodb://localhost:27017/my_blog_database";
const pass = "AdiWeynay@123";
const dbConUrl = `mongodb+srv://mongoDBUser:${pass}@cluster0.2owxn.mongodb.net/my_blog_database`;
const cookieSecret = "killer bees";
// connecting to MongoDB database
mongoose.connect(dbConUrl, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: true,
});
const app = express();
app.set("view engine", "ejs");
app.use(fileUpload());
app.use(express.static("public"));
app.use(express.json({ extended: true }));
app.use(express.urlencoded({ extended: true }));
//Form input validation custom middleware
app.use("/posts/store", validateMiddleware);
// express session middleware
app.use(
expressSession({
secret: cookieSecret,
resave: true,
saveUninitialized: true,
})
);
app.use("*", (req, res, next) => {
loggedIn = req.session.userId;
next();
});
app.use(
geolang({
siteLangs: ["en", "ti", "am"],
cookieLangName: "ulang",
})
);
app.use(
i18n({
translationsPath: path.join(__dirname, "i18n"), // <--- use here. Specify translations files path.
siteLangs: ["en", "ti", "am"],
textsVarName: "translation",
})
);
app.use(flash());
app.get("/", homeController);
app.get("/about", aboutContorller);
app.get("/contact", contactController);
app.get("/post/:id", getPostController);
app.get("/posts/new", authMiddleware, newPostController);
app.post("/posts/store", authMiddleware, storePostController);
app.get(
"/auth/user/register",
redirectIfAuthenticatedMiddleware,
newUserController
);
app.post(
"/users/register",
redirectIfAuthenticatedMiddleware,
storeUserController
);
app.get("/auth/login", redirectIfAuthenticatedMiddleware, loginController);
app.post(
"/users/login",
redirectIfAuthenticatedMiddleware,
loginUserController
);
app.get("/auth/logout", logoutController);
app.use((req, res) => {
res.render("notfound");
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 5000;
}
app.listen(port, () => {
console.log(`Server listening at ${port}...`);
});