-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
79 lines (64 loc) · 2.06 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
// bring in dependencies
const path = require("path");
const express = require("express");
const session = require("express-session");
const dotenv = require("dotenv");
const connectDB = require("./config/db");
const morgan = require("morgan");
const exphbs = require("express-handlebars");
const passport = require("passport");
const flash = require("express-flash");
const LocalStrategy = require("passport-local").Strategy;
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const User = require("./models/User");
const { MongoClient, ServerApiVersion } = require("mongodb");
const { ensureAuthenticated } = require("./middleware/authMiddleware");
// load config
dotenv.config({ path: "./config/config.env" });
// passport config
require("./config/passport")(passport);
// connect the database
connectDB();
// init app
const app = express();
app.disable("x-powered-by");
// init morgan for logging in dev mode only
if (process.env.NODE_ENV === "development") {
// register morgan middleware with app
app.use(morgan("dev"));
}
// handlebars for templating
app.engine(".hbs", exphbs.engine({ defaultLayout: "main", extname: ".hbs" }));
app.set("view engine", ".hbs");
// session
app.use(
session({
secret: process.env.SESSION_KEY,
resave: false,
saveUninitialized: false,
cookie: { maxAge: 3600000 }, //1 hour
})
);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// passport and middleware
app.use(passport.initialize());
app.use(passport.session());
//app.use(ensureAuthenticated);
// enable flash messages
app.use(flash());
// static folder set to public
app.use(express.static(path.join(__dirname, "public")));
app.use(express.static("public"));
// routes
app.use("/", require("./routes/index"));
app.use("/auth", require("./routes/auth"));
app.use("/connect", require("./routes/connect"));
// listen for incoming requests on specified port
const PORT = process.env.PORT || 3000;
app.listen(
PORT,
console.log(
`Server is running in ${[process.env.NODE_ENV]} on port ${PORT}`
)
);