-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
116 lines (99 loc) · 2.78 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
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
113
114
115
116
const express = require("express");
const http = require("http");
const exphbs = require("express-handlebars");
const mongoose = require("mongoose");
const flash = require("connect-flash");
const session = require("express-session");
const methodOverride = require("method-override");
const passport = require("passport");
const app = express();
// CONSTANTS
const MIN = 1000 * 60;
// Passport config
require("./config/passport.config")(passport);
// DB config
const db = require("./config/keys.config").DATABASE_URI;
// Connect
mongoose
.connect(db, { useNewUrlParser: true })
.then(() => console.log("MongoDB connected..."))
.catch((err) => console.log(err));
// Init Handlebars
app.engine(
"handlebars",
exphbs({
defaultLayout: "main",
layoutsDir: __dirname + "/views/layouts/",
partialsDir: __dirname + "/views/partials/",
})
);
app.set("view engine", "handlebars");
// methodOverride
app.use(methodOverride("_method"));
// Parsers
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// Express Session
app.use(
session({
secret: "secret",
resave: true,
saveUninitialized: true,
})
);
// Passport middleware
// In a Connect or Express-based application, passport.initialize() middleware
// is required to initialize Passport.
// If your application uses persistent login sessions,
// passport.session() middleware must also be used.
app.use(passport.initialize());
app.use(passport.session());
// Connect flash
app.use(flash());
// Global Vars
app.use((req, res, next) => {
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
res.locals.error = req.flash("error");
next();
});
// Redirect http to https requests
if (process.env.NODE_ENV === "production") {
app.use((req, res, next) => {
if (req.header("x-forwarded-proto") !== "https") {
res.redirect(`https://${req.header("host")}${req.url}`);
} else {
next();
}
});
}
// Ping self
// const pingSelf = () => {
// const options = {
// host: process.env.HOST,
// port: 80,
// path: "/",
// };
// http.get(options, (res) => {
// res
// .on("data", (chunk) => {
// try {
// console.log("HEROKU RESPONSE: " + chunk);
// } catch (err) {
// console.log(err.message);
// }
// })
// .on("error", function (err) {
// console.log("Error: " + err.message);
// });
// });
// };
// if (process.env.NODE_ENV === "production") {
// setInterval(pingSelf, MIN * 20);
// }
// Routes
app.use("/", require("./routes/index"));
app.use("/users", require("./routes/users.routes"));
app.use("/api/messages", require("./routes/messages.routes"));
const PORT = process.env.PORT || 5000;
app.listen(PORT, console.log(`Server started on port: ${PORT}`));