-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
49 lines (43 loc) · 1.31 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
const express = require('express');
var http = require('http');
const socket = require('socket.io')
//const { sequelizeInstance } = require('./utils/database');
const path = require("path");
const userAPI = require('./src/api/user/routes');
const { sequelize } = require('./models/index');
const bodyParser = require('body-parser');
const app = express();
const server = new http.Server(app);
const io = socket(server);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/src/index.html'));
});
const messages = [];
io.on('connection', (socket) => {
console.log(`Connecté au client ${socket.id}`);
socket.emit('messages', messages);
socket.on('message', (message) => {
if (messages.length > 10) {
messages.shift();
}
messages.push(message);
io.emit('messages', messages);
});
});
server.listen(process.env.PORT || 3000, () => {
console.log('Votre app est disponible sur localhost:3000 !');
});
userAPI.createRoutes(app);
async function main() {
try {
await sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
}
main()
// startServer();