forked from SparshJain2000/edutracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (80 loc) · 2.77 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
const express = require("express"),
app = express(),
methodOverride = require("method-override"),
mongoose = require("mongoose"),
request = require("request"),
flash = require("connect-flash"),
bodyParser = require("body-parser"),
Student = require("./models/student"),
Faculty = require("./models/faculty"),
cors = require("cors"),
passport = require("passport"),
LocalStratergy = require("passport-local"),
env = require("dotenv");
env.config();
//===================================================================
//Requiring ROUTES
var indexRoutes = require("./routes/index"),
studentRoutes = require("./routes/student"),
filesRoutes = require("./routes/files"),
facultyRoutes = require("./routes/faculty");
//===================================================================
//Connecting mongoose to mongoDB
mongoose
.connect(process.env.DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then((x) => console.log("Connected to mongo"));
//===================================================================
//linking external files (stylesheets and scripts)
app.use(express.static(__dirname + "/public"));
//for flash messages
app.use(flash());
//for post routes
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride("_method"));
//setting view engine ejs
app.set("view engine", "ejs");
//setting session
app.use(
require("express-session")({
secret: process.env.SECRET,
resave: false,
saveUninitialized: false,
}),
);
//===================================================================
//Passport initialization
//multiple user type
app.use(passport.initialize());
app.use(passport.session());
passport.use("student", new LocalStratergy(Student.authenticate()));
passport.use("faculty", new LocalStratergy(Faculty.authenticate()));
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
if (user != null) {
done(null, user);
}
});
//===================================================================
//setting locals
app.use((req, res, next) => {
res.locals.currentUser = req.user;
res.locals.err = req.flash("error");
res.locals.success = req.flash("success");
next();
});
//===================================================================
//setting up routes
app.use("/", indexRoutes);
app.use("/student", studentRoutes);
app.use("/faculty", facultyRoutes);
app.use("/files", filesRoutes);
//===================================================================
//listening at a port
const port = process.env.PORT || 7000;
app.listen(port, () => console.log("listening at " + port));
//===================================================================