-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (33 loc) · 1013 Bytes
/
server.js
File metadata and controls
38 lines (33 loc) · 1013 Bytes
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
const express = require('express');
const app = express();
var path = require('path');
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const fs = require('fs');
require('dotenv').config();
app.use(express.static("public"));
const directoryPath = path.join(__dirname, '/public');
fs.readdir(directoryPath, (err, files) => {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach((file) => {
if (file.endsWith('.html') && file != 'index.html') {
app.get('/' + file.slice(0, -5), (req, res) => {
res.sendFile(path.join(__dirname + '/public/' + file));
})
}
});
});
io.on('connection', (socket) => {
socket.on('index', (data) => {
io.emit('index', data);
});
socket.on('disconnect', () => {
console.log('User has disconnected.');
});
});
// env
http.listen(process.env.PORT || 3000, () => {
console.log('App is listening...')
})