-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
106 lines (82 loc) · 2.86 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
100
101
102
103
104
105
106
require('dotenv').config();
const express = require('express');
const flash = require('express-flash');
const session = require('express-session');
const bodyParser = require('body-parser');
const path = require('path');
const passport = require('passport');
const app = express();
const methodOverride = require('method-override');
const fs = require('fs');
const createError = require('http-errors');
const User = require('./models/User');
const mongo = require('./mongoose');
mongo.init();
const initializePassport = require('./passport-config')
initializePassport(
passport,
async(username) => User.findOne({ username: username }),
async(id) => User.findById(id)
);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(flash());
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(methodOverride('_method'))
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'views')));
app.set('view engine', 'ejs');
const routesDirectory = './routes/';
fs.readdirSync(routesDirectory).forEach((file) => {
let propsName = file.split(".")[0];
if(file.split(".").pop() === propsName) {
fs.readdirSync(path.resolve(`${routesDirectory}${path.sep}${propsName}`)).forEach((file2) => {
if(file2.split(".").pop() === 'js') {
var props = require(path.resolve(`${routesDirectory}${path.sep}${propsName}${path.sep}${file2}`))(renderTemplate, checkAuthenticated, checkNotAuthenticated, passport);
app.use(props.name, props.router);
}
})
} else {
if(file.split(".").pop() === 'js') {
var props = require(path.resolve(`${routesDirectory}${path.sep}${file}`))(renderTemplate, checkAuthenticated, checkNotAuthenticated, passport);
app.use(props.name, props.router);
}
}
})
app.use(function(req, res, next) {
next(createError(404));
});
app.use((err, req, res, next) => {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
renderTemplate(req, res, 'error');
});
function renderTemplate(req, res, template, data = {}) {
Object.assign(data, {
user: req.isAuthenticated() ? req.user : null
})
res.render(template, data);
}
function checkAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next()
}
req.flash('error', 'Vous devez être connecté, veuillez vous connecter.')
res.redirect('/login')
}
function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return res.redirect('/')
}
next()
}
app.listen('4000', () => {
console.log(`lancé sur: http://localhost:4000/`);
});