-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·99 lines (85 loc) · 2.85 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
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
let express = require("express"),
morgan = require("morgan"),
mongoose = require("mongoose"),
connect_flash = require("connect-flash"),
express_session = require("express-session"),
path = require("path"),
authRouter = require("./routes/auth"),
adminRouter = require("./routes/admin"),
eventsRoutre = require("./routes/events"),
speakerRouter = require("./routes/speaker"),
body_parser = require("body-parser"),
multer = require("multer"),
upload = multer({dest:'uploads'})
let server = express();
//// connect mongo
mongoose.connect("mongodb://localhost:27017/nodedb").then(()=>{console.log("db connected .....")})
.catch((error)=>{
console.log("db error "+error);
})
server.set("view engine","ejs");
server.set("views",path.join(__dirname,"views"));
// middle wares
server.use(morgan("short"));
// time MW //
server.use((request,response,next)=>{
let time = (new Date ()).getFullYear();
// console.log(time);
if (time == "2019"){
next();
}
else{
next(new Error("time error"));
}
});
server.use(express.static(path.join(__dirname,"public")));
server.use(express.static(path.join(__dirname,"uploads")));
server.use(express.static(path.join(__dirname,"node_modules","bootstrap","dist")));
server.use(express.static(path.join(__dirname,"node_modules","jquery","dist")));
// all MW
server.use(/\//,(request,response)=>{
response.redirect("/login");
});
server.use(express_session({secret : "wafik"}));
server.use(connect_flash());
server.use(body_parser.urlencoded());
server.use(body_parser.json());
server.use(authRouter);
/////////////////////////////// session check //////////
server.use((request,response,next)=>{
if(request.session.userName){
response.locals.userName = request.session.userName;
next();
}else{
request.flash("msg","session ended ");
response.redirect("/login");
}
})
server.use("/speaker",speakerRouter);
server.use((request,response,next)=>{
if(request.session.userName == "wafik"){
next();
}
else if(request.session.userName){
// response.locals.userName = request.session.userName;
request.flash("msg"," you are not an admin ..... ");
response.redirect("/login");
}
else{
request.flash("msg","session ended ");
response.redirect("/login");
}
});
server.use("/admin",adminRouter);
server.use("/events",eventsRoutre);
server.use((request,response)=>{
response.send("<h1> not found </h1>")
});
// Err MW
server.use((error,request,response,next)=>{
console.log("Error" +error)
});
// listen
server.listen(8080,()=>{
console.log("wafik listening................................")
});