-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
172 lines (142 loc) · 4.22 KB
/
index.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
var express = require("express"),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
indexPage = require("./routes/index"),
assignmentPage = require("./routes/assignment")
quizPage = require("./routes/quiz")
path = require('path'),
fs = require('fs'),
multer = require('multer'),
methodOverride = require("method-override"),
app = express(),
assignmentModel = require('./models/assignment'),
submitassignmentModel = require('./models/submitassignment'),
quizModel = require('./models/quiz');
flash = require("connect-flash");
const PORT = process.env.PORT || 8000;
// auth imports
var passport = require('passport');
var LocalStrategy = require('passport-local');
var User = require('./models/user');
// mongo config
const MONGODB_URL = 'mongodb+srv://AMU_VLAB_ADMIN:[email protected]/myFirstDatabase?retryWrites=true&w=majority'
mongoose.connect(MONGODB_URL, { useNewUrlParser: true, useUnifiedTopology: true });
// app.use exps
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(methodOverride("_method"));
app.use(flash());
// express session
app.use(require('express-session')({
secret: "The snake is flying on pencil nose!!",
resave: false,
saveUninitialized: false
}));
// Passport config
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function (req, res, next) {
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
// console.log(res.locals.currentUser);
next();
});
// getting routes form routes dir
app.use(indexPage);
// uplaoding assignment content
var teacherstorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './public/uploads/teacher')
},
filename: (req, file, cb) => {
cb(null, file.fieldname + '-' + Date.now())
}
});
var studentstorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './public/uploads/students')
},
filename: (req, file, cb) => {
cb(null, file.fieldname + '-' + Date.now())
}
});
var upload = multer({ storage: teacherstorage });
var studentupload = multer({ storage: studentstorage });
app.use(function (req, res, next) {
if (!req.body["expvalue"])
next();
else {
res.locals.expname = req.body["expvalue"];
next();
}
});
// assignment routes!
app.use(assignmentPage)
//Downloading the assignment
app.get('/download/:id', (req, res) => {
assignmentModel.find({ _id: req.params.id }, (err, item) => {
if (err) {
console.log(err)
}
else {
var path = __dirname + '/public/' + item[0].img;
res.download(path);
}
});
});
app.get('/downloadsubmittedassignment/:id',(req,res)=>{
submitassignmentModel.find({_id:req.params.id},(err,item)=>{
if(err){
console.log(err)
}
else{
var path= __dirname+'/public/'+item[0].img;
res.download(path);
}
});
});
app.get("/assignment/:id", function (req, res) {
assignmentModel.findById(req.params.id, function (err, assignment) {
if (err) {
res.redirect("/assignment");
} else {
res.render("assignmentdetails", { assignment: assignment });
}
})
app.use(function (req, res, next) {
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
});
app.delete("/assignment/:id", function (req, res) {
assignmentModel.findByIdAndRemove(req.params.id, function (err) {
if (err) {
res.redirect("/assignment");
} else {
res.redirect("/assignment");
}
})
});
app.delete("/submitassignment/:id", function (req, res) {
submitassignmentModel.findByIdAndRemove(req.params.id, function (err) {
if (err) {
res.redirect("/submitassignment");
} else {
res.redirect("/submitassignment");
}
})
});
app.use(quizPage);
mongoose.connection.on("connected", () => {
console.log("connected");
});
app.listen(PORT, function () {
console.log("Welcome you to AMUVLAB");
});
module.exports = app;