-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
112 lines (93 loc) · 2.7 KB
/
server.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 path = require("path");
const express = require("express");
const expressLayouts = require("express-ejs-layouts");
const passport = require("passport");
const mongoose = require("mongoose");
const flash = require("connect-flash");
const session = require("express-session");
const cron = require("node-cron");
require("dotenv/config");
const app = express();
// Passport Config
require("./config/passport")(passport);
// import routes
const homeRoute = require("./routes/home");
const termsRoute = require("./routes/terms");
const apiRoute = require("./routes/api");
const docsRoute = require("./routes/docs");
const loginRoute = require("./routes/auth");
const userRoute = require("./routes/user");
const newsletterRoute = require("./routes/newsletter");
const User = require("./models/User");
// Static
app.use(express.static(path.join(__dirname, "public")));
// EJS
app.use(expressLayouts);
app.set("view engine", "ejs");
app.set("layout login", false);
app.set("layout docs", false);
app.set("layout error404", false);
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
// connect to db
mongoose
.connect(process.env.DB_CONNECTION, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
})
.then(() => console.log("DB Connected..."))
.catch((err) => console.log(err));
// Express session
app.use(
session({
secret: "secret",
resave: true,
saveUninitialized: true,
})
);
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// Connect flash
app.use(flash());
// Global variables
app.use(function (req, res, next) {
res.locals.user = req.user;
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
res.locals.error = req.flash("error");
next();
});
app.use("/newsletter", newsletterRoute);
app.use("/api", apiRoute);
app.use("/docs", docsRoute);
app.use("/user", userRoute);
app.use("/", loginRoute);
app.use("/", termsRoute);
app.use("/", homeRoute);
// Custom middleware for handling 404 errors
app.use((req, res, next) => {
res.status(404).render('error404', { title: '404 - Not Found', layout: false});
});
// Custom middleware for handling 500 errors
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).render('error500', { title: '500 - Internal Server Error', layout: false });
});
// reset the daily usage of the user at midnight.
cron.schedule("0 0 0 * * *", async () => {
const users = await User.find();
users.forEach((user) => {
user.dailyRequest = 0;
user.save();
});
});
const PORT = process.env.port || 3000;
app.listen(PORT, "0.0.0.0", () => {
console.log("server is up and running already...");
});