-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
177 lines (140 loc) · 5.96 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const session = require("express-session");
const passport = require("passport");
const cors = require("cors");
const mongoose = require("mongoose");
require("dotenv").config();
const port = process.env.PORT || 5000;
const app = express();
app.use(cors());
/*------- Setup CORS ----------*/
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, "../AMS/build")));
}
else {
app.use(
cors({
origin: "http://localhost:3000", // allow to server to accept request from different origin
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true // allow session cookie from browser to pass through
})
);
}
/*--------- CORS Setup Successfully-------*/
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
/* -------- Set up session ------------*/
app.use(session({
secret: "AMS app for SOE project.",
resave: false,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
/* -------- Session set up ended ------------*/
/* -------- MongoDB Connection --------------*/
// var MONGODB_URI = "";
// MONGODB_URI = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASS}@buildone.ttypp.mongodb.net/amsDB?retryWrites=true&w=majority`
// mongoose.connect(MONGODB_URI, {useNewUrlParser: true, useUnifiedTopology: true});
mongoose.connect('mongodb://localhost:27017/gurukulDB', {useNewUrlParser: true, useUnifiedTopology: true});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'Connection error:'));
db.once('open', function(){
console.log("MongoDB Local Server Started!");
});
/* -------- MongoDB Connection Setup Ended ---- */
/* -------- Serializing the User ------------ */
const User = require("./models/roles/user");
passport.use(User.createStrategy());
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
/* ------------ Serializing/Deserializing Ends ------------- */
const loginRoute = require("./routes/Auth/Login");
const registerRoute = require("./routes/Auth/Register");
const Profile = require("./routes/Profile/viewProfile");
const EditProfile = require("./routes/Profile/editProfile");
const isLoggedIn = require("./routes/Auth/isLoggedIn");
const logRoute = require("./routes/AdminTasks/viewLog");
const classRoute = require("./routes/AdminTasks/createClass");
const courseRoute = require("./routes/AdminTasks/createCourse");
const calendarRoute = require("./routes/AdminTasks/updateCalendar");
const logoutRoute = require("./routes/Auth/Logout");
const viewCalendar = require("./routes/General/viewCalendar");
const getCourses = require("./routes/FacultyTasks/getCourses");
const uploadMarks = require("./routes/FacultyTasks/setMarks");
const upAttendance = require("./routes/FacultyTasks/markAttendance");
const viewMarks = require("./routes/StudentTasks/viewMarks");
const viewPresent = require("./routes/StudentTasks/viewPresent");
const viewAbsent = require("./routes/StudentTasks/viewAbsent");
const viewUsers = require("./routes/AdminTasks/allUsers");
const studClass = require("./routes/AdminTasks/updateClass");
const studCourse = require("./routes/AdminTasks/updateCourse");
const sendAlert = require("./routes/AdminTasks/sendAlert");
const sendClass = require("./routes/StudentTasks/sendClass");
const sendCourse = require("./routes/StudentTasks/sendCourse");
const notifRoute = require("./routes/General/getNotifs");
const makeNotifs = require("./routes/General/makeNotifs");
const allNotifs = require("./routes/General/allNotifs");
const myCourse = require("./routes/StudentTasks/getCourses");
const allClasses = require("./routes/AdminTasks/allClasses");
const allCourses = require("./routes/AdminTasks/allCourses");
const uploadPic = require("./routes/Profile/uploadPicture");
const uploadFile = require("./routes/StudentTasks/uploadFile");
const updateUser = require("./routes/AdminTasks/updateUser");
const uploadClassTime = require("./routes/AdminTasks/uploadClassTime");
const uploadCourseTime = require("./routes/AdminTasks/uploadCourseTime");
const viewTime = require("./routes/General/viewTime");
const getStudents = require("./routes/FacultyTasks/getStudents");
/*------ App Config---------------*/
// For all users
app.use("/api/login", loginRoute);
app.use("/api/register", registerRoute);
app.use("/api/profile", Profile);
app.use("/api/profile/edit", EditProfile);
app.use("/api/isloggedin", isLoggedIn);
app.use("/api/calendar", viewCalendar);
app.use("/api/logout", logoutRoute);
app.use("/api/notifs", notifRoute);
app.use("/api/notifs/all", allNotifs);
app.use("/api/makenotifs", makeNotifs);
app.use("/api/uploadpic", uploadPic);
app.use("/api/viewtime", viewTime);
// For Admin only
app.use("/api/logs", logRoute);
app.use("/api/addclass", classRoute);
app.use("/api/addcourse", courseRoute);
app.use("/api/addcalendar", calendarRoute);
app.use("/api/class", studClass);
app.use("/api/course", studCourse);
app.use("/api/getclasses", allClasses);
app.use("/api/allcourses", allCourses);
app.use("/api/updateuser", updateUser);
app.use("/api/class/uploadtime", uploadClassTime);
app.use("/api/course/uploadtime", uploadCourseTime);
app.use("/api/allusers", viewUsers);
app.use("/api/sendalert", sendAlert);
// For Faculty only
app.use("/api/addmarks", uploadMarks);
app.use("/api/addattend", upAttendance);
app.use("/api/getcourses", getCourses);
app.use("/api/getstudents", getStudents);
// For Student only
app.use("/api/viewmarks", viewMarks);
app.use("/api/viewpresent", viewPresent);
app.use("/api/viewabsent", viewAbsent);
app.use("/api/sendclass", sendClass);
app.use("/api/sendcourse", sendCourse);
app.use("/api/mycourse", myCourse);
app.use("/api/uploadfile", uploadFile);
/*------ App Config Done--------- */
app.listen(port, function() {
console.log("Server started at port 5000");
});