-
Notifications
You must be signed in to change notification settings - Fork 0
/
trenodeServer.js
53 lines (44 loc) · 1.46 KB
/
trenodeServer.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
const express = require('express');
const compression = require('compression');
const session = require('express-session');
const fs = require('fs');
const app = express();
// Enabling middlewares
// Using compression to compress the body of all the requests
app.use(compression());
// Using express-session to implement sessions
app.use(session({
secret: 'NCpPT3LyxSq5z8n52YPQsVrWvbvyBLSZ',
name: 'SessionId',
resave: false,
// rolling: true,
saveUninitialized: true,
// cookie: {
// maxAge: 600000
// }
}));
// Setting parameters for the express app
// Setting path for public files like css and js libraries
app.use('/public', express.static(__dirname + '/public'));
// Setting the engine for handling templates
app.set('view engine', 'ejs');
// Setting the template folder
app.set('views', __dirname + '/views');
// Setting the body parser for the requests
app.use(express.json());
// Loading router automatically based on the files in the folder
const routesFiles = fs.readdirSync('./routes').filter(file => file.endsWith('.js'));
for (const file of routesFiles) {
const route = require('./routes/' + file);
app.use(route.name, route.router);
}
// Redirects all non-existent page requests to the index
app.use(function(req, res) {
res.redirect('/');
});
// Activates the server
const server = app.listen(8080, function() {
const host = server.address().address;
const port = server.address().port;
console.log('Treni app listening at http://%s:%s', host, port);
});