-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
58 lines (42 loc) · 1.38 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
require('./api/data/db.js');
var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
var session = require('express-session');
var routesBack = require('./api/routes/routesback.js');
var routesPublic = require('./api/routes/routespublic.js');
// Define the port to run on
app.set('port', 3000);
app.use(session({
secret: 'supersecret',
resave: true,
saveUninitialized: true,
httpOnly: false,
secure: false
}));
// Add middleware to console log every request
app.use(function(req, res, next) {
console.log(req.method, req.url);
// Si on essai d'accéder à la home sans session
if(!req.session.name && req.path === '/dashboard.html') {
res.redirect(302, '/');
// On redirige vers la connexion
}
next();
});
// Set static directory before defining routes
app.use(express.static(path.join(__dirname, 'public')));
app.use('/node_modules', express.static(__dirname + '/node_modules'));
app.use('/fonts', express.static(__dirname + '/fonts'));
// Enable parsing of posted forms
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Add some routing
app.use('/api', routesBack);
app.use('/', routesPublic);
// Listen for requests
var server = app.listen(app.get('port'), function() {
var port = server.address().port;
console.log('Magic happens on port ' + port);
});