-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
102 lines (85 loc) · 3.65 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
// -------------------------------
// LOAD NECESSARY LIBS - CHARGER LES LIBRAIRIES NECESSAIRES
// -------------------------------
const express = require('express')
const hbs = require("hbs") // Handlebars framework
const cors = require('cors') // Cors front end access
const session = require("express-session") // Node.js sessions ( No JWT)
const fs = require('fs') // File system management
const bodyParser = require('body-parser')
// -------------------------------
// LOAD APP CONFIG - ( DATABASE TOKENS , FTP TOKEN, CLOUDINARY TOKEN, MONGODB TOKEN, etc ...) - CHARGER LA CONFIGURATION DE L'APPLICAITON NODE.JS
// -------------------------------
const config = require("./config.json")
// -------------------------------
// START APP
// -------------------------------
const app = express()
// -------------------------------
// PORT
// -------------------------------
const port = process.env.PORT || 5000 // needed for heroku : https://stackoverflow.com/questions/28706180/setting-the-port-for-node-js-server-on-heroku
// -------------------------------
// USERS NODE SESSIONS - SESSIONS UTILISATEURS
// -------------------------------
app.use(session({ secret: "ssshhhhh", saveUninitialized: true, resave: true }))
// -------------------------------
// POSTS QUERIES NEEDS THIS
// -------------------------------
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
// -------------------------------
// CORS - ON DECLARE UTILISER CORS
// -------------------------------
app.use(cors())
// -------------------------------
// SETTING HANDLEBARS VIEW ENGINE AND VIEWS DIRECTORY, ON CHOISIT LE MOTEUR DE TEMPLATES et INDIQUE LE REPERTOIRE QUI CONTIENT LES VUES HBS
// -------------------------------
app.set('view engine', 'hbs')
app.set('views', './views')
// -------------------------------
// USING HANDLEBARS HTML PARTIALS -> SET DIRECTORY - ON DECLARE LE REPERTOIRE DES TEMPLATES HTML PARTIELS
// -------------------------------
hbs.registerPartials(__dirname + "/views/partials")
// -------------------------------
// SWITCH LOG IN TO LOG OUT IN NAVBAR
// -------------------------------
global.auth = false; //this var is only used in the NavBar, for switching from log in to log out menu, his value is modified by login.js
hbs.registerHelper('auth', function() {
if(global.auth == false){
return '<a class="nav-link" href="../login"> <i class="bi bi-person-circle"></i> Login</a>'
}else{
return '<a class="nav-link" href="../logout"> <i class="bi bi-person-circle"></i> Logout</a>'
}
})
// -------------------------------
// CHANGE APP THEME
// -------------------------------
global.theme = config.theme ;
hbs.registerHelper('theme', function() {
return global.theme
});
// -------------------------------
// Static images and css directory routes
// -------------------------------
app.use(express.static(__dirname + '/public'))
app.use(express.static(__dirname + '/public/images'))
app.use(express.static(__dirname + '/public/css'))
app.use(express.static(__dirname + '/public/themes'))
// -------------------------------
// Classical express routes
// -------------------------------
app.use('/home', require('./routes/home'))
app.use('/list', require('./routes/list'))
app.use('/crud', require('./routes/crud'))
app.use('/contact', require('./routes/contact'))
app.use('/upload', require('./routes/upload'))
app.use('/login', require('./routes/login'))
app.use('/logout', require('./routes/logout'))
app.use('/error403', require('./routes/error403'))
app.get('/', (req, res) => {
res.send('Hello World!')
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}!`)
});