-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·147 lines (103 loc) · 3.34 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var CONFIG = require('./config.js');
var path = require('path');
var express = require('express')();
var server = require('http').Server(express);
var io = require('socket.io')(server);
var serveStatic = require('serve-static');
var fs = require("fs");
var bodyParser = require('body-parser');
var url = require('url');
var cookieParser = require('cookie-parser');
express.use(serveStatic( __dirname + '/web/' ));
express.use(require('express-favicon-short-circuit'));
express.use(cookieParser());
express.use(bodyParser.urlencoded({
extended: true
}));
express.set( 'view engine', 'ejs' );
let DB_T = require('./database/t.json');
let DB_USERS = require('./database/users.json');
/*
express.use(function (req, res, next) {
console.log(req.path);
if (req.path.startsWith('/login')) {
next();
} else {
if (req.cookies['username'] == undefined) {
res.render( __dirname + '/web/resources/forbidden', {});
} else {
next();
}
}
});
*/
express.get('/login/:username/:password', function(req, res) {
var username = req.params.username;
var password = req.params.password;
res.cookie('username', username, { maxAge: 900000, httpOnly: true });
res.redirect('/');
});
express.get('/', function (req, res) {
console.log(req.query);
console.log('>> loading jardin');
res.render( __dirname + '/web/services/n/n', {
BASEURL : CONFIG.site.baseURL
});
});
express.get('/:service/', function (req, res) {
var service = req.params.service;
console.log('>> loading service:', service);
res.render( __dirname + '/web/services/'+service+'/'+service, {
BASEURL : CONFIG.site.baseURL,
service : service
});
});
express.get('/:service/:page', function (req, res) {
var service = req.params.service;
var page = req.params.page;
console.log('>> loading service:', service);
res.render( __dirname + '/web/services/'+service+'/pages/'+page, {
BASEURL : CONFIG.site.baseURL,
service : service
});
});
/////////////////////////
io.on('connection', function(socket) {
console.log('New socket connected');
// io.sockets.emit('connectedId',data);
socket.emit('connection/handshake', {});
socket.on('chat message', function(msg){
console.log('message: ' + msg);
io.emit('chat message', msg);
});
socket.on('connection/handshake', function(data) {
console.log(data);
// console.log('connectedId : '+data.id);
/*
socket.id = data.id;
socket.join('/'+socket.id);
*/
// io.sockets.emit('connectedId',data);
// addUser();
io.sockets.emit('users/update', { DB_USERS });
});
socket.on('showSlide', function(data) {
io.sockets.emit('showSlide',data);
});
socket.on('refresh', function(data) {
console.log('refresh');
io.sockets.emit('refresh',null);
});
socket.on('disconnect', function(){
});
});
server.listen(CONFIG.site.port, function(){
console.log('>> accédez à r3bo0t à l\'adresse [ localhost:'+CONFIG.site.port+' ]');
});
function saveDatabase(dbName, data) {
let dataJson = JSON.stringify(data, null, 2);
fs.writeFile('./database/'+dbName+'.json', dataJson, (err) => {
if (err) throw err;
console.log('[SAVE] Database `'+dbName+'.json` saved.');
});
}