-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (63 loc) · 2.01 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
import express from "express";
import bodyParser from "body-parser";
import * as dotenv from "dotenv";
import cors from "cors";
import db from "./db.config.js";
import morgan from "morgan";
import newsRouter from "./routes/news.routes.js";
import workshopRouter from "./routes/workshop.routes.js";
import postsRouter from "./routes/post.routes.js";
import courseRouter from "./routes/course.routes.js";
import teamRouter from "./routes/team.routes.js";
import projectRouter from "./routes/project.routes.js";
import adminRouter from "./admin/adminRoutes.js";
import passport from "./passport.config.js";
import session from "express-session";
import userRouter from "./routes/user.routes.js";
import path from "path";
import { fileURLToPath } from "url";
import { dirname } from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
dotenv.config();
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(morgan("dev"));
app.use(cors());
app.use("/uploads", express.static(path.join(__dirname, "uploads")));
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
})
);
app.use(passport.initialize());
app.use(passport.session());
db();
function checkAuthenticated(req, res, next) {
if (req.baseUrl === "/admin") return next();
if (req.isAuthenticated()) {
return next();
}
res.redirect("/admin");
}
app.use("/news", newsRouter);
app.use("/workshops", workshopRouter);
app.use("/posts", postsRouter);
app.use("/courses", courseRouter);
app.use("/team", teamRouter);
app.use("/projects", projectRouter);
app.use("/admin", checkAuthenticated, adminRouter);
app.use("/user", userRouter);
app.listen(
process.env.PORT ? process.env.PORT : 8080,
process.env.HOST ? process.env.HOST : "127.0.0.1",
console.log(
`listening on http://localhost:${
process.env.PORT ? process.env.PORT : 8080
}/`
)
);