-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (89 loc) · 3.1 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
const express = require("express");
const app = express();
const path = require("path");
const PORT = 4444;
const bodyparser = require("body-parser");
const hbs = require("hbs");
const mongoose = require("mongoose");
const MongoStore = require("connect-mongo");
const session = require("express-session");
const passport = require("passport");
const isLoggedIn = require("./middlewares/isLoggedIn");
require("dotenv").config();
const moment = require("moment");
hbs.registerPartials(__dirname + "/views/partials");
app.set("view engine", "hbs");
app.set("views", path.join(__dirname, "views"));
// Session middleware configuration
app.use(
session({
secret: "hgsadjkhsa",
resave: false,
saveUninitialized: true,
store: MongoStore.create({ mongoUrl: process.env.MONGO_URL }),
})
);
// --> Middlewares
app.use(bodyparser.urlencoded({ extended: true }));
app.use(bodyparser.json());
app.use(express.static(path.join(__dirname, "public")));
require("./utilities/passport");
app.use(passport.initialize());
app.use(passport.session());
// --> Routes
app.get("/", require("./routes/generalRoutes"));
app.get("/signup", require("./routes/generalRoutes"));
app.post("/signup", require("./routes/generalRoutes"));
// --> Passport Routes
app.post("/login", passport.authenticate("local", { failureRedirect: "/" }), require("./routes/generalRoutes"));
app.get("/auth/facebook", passport.authenticate("facebook"));
app.get("/auth/facebook/callback", passport.authenticate("facebook", { failureRedirect: "/" }), function (req, res) {
res.redirect("/app");
});
app.get("/auth/google", passport.authenticate("google", { scope: ["profile"] }));
app.get("/auth/google/callback", passport.authenticate("google", { failureRedirect: "/" }), function (req, res) {
res.redirect("/app");
});
// --> All APP routes mounted here
app.use("/app", isLoggedIn, require("./routes/appRoutes"));
// Catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error("Not Found");
err.status = 404;
next(err);
});
// --> Error Handling middleare
app.use((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", { error: err });
});
// --> HBS helper function
// Register a helper for date formatting
hbs.registerHelper("formatDate", function (dateString) {
const date = moment(dateString);
return date.format("MMM DD, YYYY - hh:mm A"); // Formats date as "Feb 06, 2024 - 11:47 PM"
});
// Defining a helper function to compare values
hbs.registerHelper("isEqual", function (a, b, options) {
if (a === b) {
return options.fn(this); // If values are equal, execute the block
} else {
return options.inverse(this); // If values are not equal, execute the else block
}
});
// --> Database Connection
mongoose
.connect(process.env.MONGO_URL)
.then(() => {
console.log("Connected to DB");
})
.catch((err) => {
console.log(err);
});
app.listen(PORT, () => {
console.log(`http://localhost:` + PORT);
});