-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
121 lines (101 loc) · 2.81 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
require("./db/projectdb");
require("./models/User");
require('dotenv').config();
const express = require('express');
const expressLayouts = require('express-ejs-layouts');
var mongoose = require("mongoose");
var assign = mongoose.model("Assignment");
var student = mongoose.model("Student");
const passport = require('passport');
const flash = require('connect-flash');
const session = require('express-session');
const dclass = mongoose.model("Class");
const user = mongoose.model("User");
const { ensureAuthenticated, forwardAuthenticated } = require("./config/auth");
const app = express();
app.use(express.static("public"));
// Passport Config
require('./config/passport')(passport);
// DB Config
const db = require('./config/keys');
// EJS
app.use(expressLayouts);
app.set('view engine', 'ejs');
// Express body parser
app.use(express.urlencoded({ extended: true }));
// Express session
app.use(
session({
secret: 'secret',
resave: true,
saveUninitialized: true
})
);
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// Connect flash
app.use(flash());
// Global variables
app.use(function (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();
});
// Routes
app.use('/', require('./routes/index.js'));
app.use('/users', require('./routes/users.js'));
const assignment = require("./assignment/app.js");
const homework = require("./homework/app.js");
app.use("/assignment", ensureAuthenticated ,assignment);
app.use("/homework",ensureAuthenticated , homework);
app.get("/discussion/:cid/:id/", async (req, res) => {
const cid = req.params.cid;
try {
const data = await dclass.findById(cid);
res.render("post_something", { cid: cid, id: req.params.id });
}
catch (e) {
console.log(e);
}
});
app.post("/discussion/:cid/:id/", async (req, res)=>{
try {
var cid = req.params.cid;
var id = req.params.id;
const data = await user.findById(id);
//console.log(data.name);
const cdata = await dclass.findOneAndUpdate({ "_id": cid },
{
$push:
{
"discussion": [{
"sender_id" : data._id,
"sender_name" : data.name,
"msg": req.body.msg,
}]
},
$currentDate :
{
"discussions" : [{
date : true,
}]
}
});
console.log(cdata);
if(id === cdata.fc_id){
const url = "/assignment/" + cid + "/" + id + "/";
res.redirect(url);
}
else{
const url = "/homework/" + cid + "/" + id + "/";
res.redirect(url);
}
}
catch (e) {
console.log(e);
}
});
const PORT = process.env.PORT || 9000;
app.listen(PORT, console.log(`Server started on port ${PORT}`));