This repository was archived by the owner on Jan 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
87 lines (66 loc) · 2.68 KB
/
server.js
File metadata and controls
87 lines (66 loc) · 2.68 KB
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
const express = require("express");
const bodyParser = require("body-parser");
const fs = require("fs");
const convertSass = require("sass-folder-converter");
//TODO : https://www.npmjs.com/package/express-mailer
// Convert SASS to CSS :
convertSass(__dirname + "/public/sass/", __dirname + "/public/css/");
// Create express instance :
global.server = express();
// Setup the logger :
server.use(require("morgan")("dev"));
// Define public folder :
server.use(express.static(__dirname + "/public"));
// Setup views folder :
server.set("views", __dirname + "/views");
// Setup view engine :
server.set("view engine", "ejs");
// Setup sessions and cookies :
server.use(require("cookie-parser")());
server.use(require("express-session")({
secret: "09e60df3-e2d7-4c10-b103-380da8d5719b",
resave: false,
saveUninitialized: true,
cookie: {
secure: false // set in true if the website use https
}
}));
// Add body parser middleware for get body content (for post method) :
server.use(bodyParser.json()); // support json encoded bodies
server.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
// Middleware for secure pages access :
/**
* The privilege system uses the privileges.json configuration to set the accesses to the different pages as well as the page that allows the connection.
*
* If no "login-route" key is specified, the visitor will be redirected to the root of the site.
*
* To add permissions to a visitor, you have to go through the sessions, there is an array with the key "privileges" in it, this is where you have to add the permissions.
*
* Example for add "ADMIN" permission : request.session.privileges.push("ADMIN");
*/
let privileges = JSON.parse(fs.readFileSync("privileges.json", {encoding: "utf-8"}));
server.use((request, response, next) => {
if(!request.session.privileges) request.session.privileges = [];
for(let [key, value] of Object.entries(privileges)){
if(request.session.privileges.includes(key)) continue;
for(let routeKey in value["routes-access"]){
if(request.url.startsWith("/" + value["routes-access"][routeKey])){
if(value["login-route"]){
response.redirect("/" + value["login-route"]);
} else {
response.redirect("/");
}
return;
}
}
}
next();
});
// Routes and 404 error :
fs.readdirSync(__dirname + "/routes/").forEach(fileName => require("./routes/" + fileName)); //TODO: ajout du support des sous dossier.
server.get("*", (request, response) => {
response.render("error");
response.status(404);
});
// Listen port :
server.listen(3000);