-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
107 lines (72 loc) · 1.9 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
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const expressSession=require('express-session');
const SessionStore=require('connect-session-sequelize')(expressSession.Store);
const csrf=require('csurf');
const flash=require('connect-flash');
const db=require('./models/index');
const store=new SessionStore({
db:db.sequelize
});
const path = require("path");
const userRouter=require('./Routes/User')
const adminRouter=require('./Routes/Admin');
const authRouter=require('./Routes/Auth')
const errorRouter=require('./Routes/Errors');
const models=require('./models/index')
app.set("views", path.join(__dirname, "Views"));
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "Public")));
app.use('/images',express.static(path.join(__dirname, "images")));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
app.use(expressSession({
store,
secret:'this is a secret',
resave:false,
saveUninitialized:false
})
)
app.use(csrf());
app.use(flash());
app.use((req,res,next)=>{
res.locals.isAuth=req.session.userId;
res.locals.csrfToken=req.csrfToken();
next();
})
app.use((req, res, next) => {
if(!req.session.userId){
return next()
}
models.User.findByPk(req.session.userId)
.then((user) => {
if(!user){
return next();
}
req.user = user;
next();
})
.catch((err) => {
const error=new Error(err);
next(error);
});
});
app.use(userRouter);
app.use(authRouter);
app.use('/Admin',adminRouter);
app.use(errorRouter.get404);
app.use((error,req,res,next)=>{
console.log(error);
res.render('./ErrorsPages/500');
});
(async function(){
try{
await db.sequelize.authenticate();
console.log('conected');
await db.sequelize.sync();
app.listen(process.env.PORT || 5000);
}catch(err){
console.log(err);
}
})();