-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (96 loc) · 3.11 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
112
const bodyParser = require("body-parser");
const path = require("path");
const express = require("express");
const dbconnect = require("./database/dbconnect");
// const jwt = require("jsonwebtoken");
const methodOverride = require("method-override");
const cookieparser = require("cookie-parser");
const session = require("express-session");
const mongosession = require("connect-mongodb-session")(session);
const morgan = require("morgan");
const dotenv = require("dotenv");
dotenv.config();
const app = express();
// Your MongoDB connection URI
const mongoURI =
"mongodb+srv://Vasanth:[email protected]/?retryWrites=true&w=majority";
dbconnect(mongoURI);
app.use(methodOverride("_method"));
app.use(cookieparser());
// Parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// Parse application/json
app.use(bodyParser.json());
// Set the view engine and views directory
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.static("public"));
app.use("/uploads", express.static("uploads"));
const store = new mongosession({
uri: mongoURI,
collection: "mysession",
});
app.use(
session({
key: "keybro",
secret: "VASANTH###",
saveUninitialized: false,
resave: false,
cookie: {
// secure: true,
// httpOnly: true,
expires: 1000 * 60 * 60,
},
store: store,
})
);
const adminAuth = (req, res, next) => {
if (req.session.adminlogin) {
next();
} else {
res.redirect("/login");
}
};
const isAuth = (req, res, next) => {
if (req.session.isAuth) {
next();
} else {
res.redirect("/login");
}
};
const loginAuth = (req, res, next) => {
if (!req.session.isAuth) {
next();
} else {
res.redirect("/dashboard");
}
};
const login = require("./routes/login");
const home = require("./routes/home");
const signup = require("./routes/signup");
const dashboard = require("./routes/dashboard");
const posts = require("./routes/posts");
const logout = require("./routes/logout");
const postuploaded = require("./routes/postuploaded");
const view_all_posts = require("./routes/view-all-posts");
const view_blog = require("./routes/view-blog");
const updateBlog = require("./routes/userupdate");
const deletepost = require("./routes/deletePost");
const profile = require("./routes/profile");
const admindashboard = require("./routes/admindasboard");
const postview = require("./routes/postview")
app.use("/", home);
app.use("/login", loginAuth, login);
app.use("/logout", logout);
app.use("/signup", loginAuth, signup);
app.use("/dashboard", isAuth, dashboard);
app.use("/dashboard/post", isAuth, view_all_posts);
app.use("/dashboard/createpost", isAuth, posts);
app.use("/dashboard/successUploaded", isAuth, postuploaded);
app.use("/dashboard/view-blog", isAuth, view_blog);
app.use("/dashboard/update-blog", isAuth, updateBlog);
app.use("/dashboard/deletepost", isAuth, deletepost);
app.use("/dashboard/profile", isAuth, profile);
app.use("/admindashboard", adminAuth, admindashboard);
app.use("/postview", postview);
app.listen(5000, () => console.log("Running at 5000"));