-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
72 lines (57 loc) · 1.94 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
//loading required modules
const express = require("express");
const app = express();
const expressLayouts = require("express-ejs-layouts");
const mongoose = require("mongoose");
const flash = require("connect-flash");
const session = require("express-session");
const passport = require("passport");
// const path = require("path");
//localhost port
let PORT = 2000;
//Passport config
require("./config/passport")(passport);
//DB Config
const db = require("./config/keys").MongoURI;
//Connect to Mongo
mongoose
.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("MongoDB Connected..."))
.catch((err) => console.log(err));
//EJS
app.use(expressLayouts);
app.set("view engine", "ejs");
//Bodyparser (used to read body portion of incoming request stream)
app.use(express.urlencoded({ extended: false }));
//Express Session (generates session cookies to match with data stored server side i.e logged-in user)
app.set("trust proxy", 1); // trust first proxy
app.use(
session({
secret: "rumour",
resave: true,
saveUninitialized: true,
})
);
//Passport middleware
app.use(passport.initialize());
app.use(passport.session());
//Flash session (used when pages are being re-rendered to display messages )
app.use(flash());
//Global variables (since messages will have different colours)
app.use((request, response, next) => {
//to set a global variable, do response.locals.newVariable
response.locals.success_msg = request.flash("success_msg");
response.locals.error_msg = request.flash("error_msg");
response.locals.error = request.flash("error");
next();
});
//Routes
app.use("/", require("./routes/index"));
app.use("/users", require("./routes/users"));
//app.use(express.static(path.join(__dirname, "./static")));
/* app.get("/", (request, response) => {
response.sendFile(path.join(__dirname, "./login.html"));
}); */
app.listen(PORT, () => {
console.log(`Express is listening at port ${PORT}`);
});