-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
41 lines (32 loc) · 1.23 KB
/
index.mjs
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
import express from 'express';
import http from 'http';
import socketIo from 'socket.io';
import apiHelperController from './controllers/apiHelperController.mjs';
import dotenv from 'dotenv';
dotenv.config(); // This loads environment variables from the .env file into `process.env`
const app = express();
const server = http.createServer(app);
const io = socketIo(server); // Initialize Socket.IO with the HTTP server
const port = process.env.PORT || 3000;
app.use(express.json());
// Define your routes
app.get('/vault-data', apiHelperController.getVaultData);
// Socket.IO connection event
io.on('connection', (socket) => {
console.log('A user connected');
// Listen for messages from the client
socket.on('request-data', async () => {
try {
const data = await apiHelperController.getVaultData(); // You might need to adjust this call
socket.emit('data-response', data); // Send the data back to the client
} catch (error) {
socket.emit('data-error', { error: error.message });
}
});
socket.on('disconnect', () => {
console.log('User disconnected');
});
});
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});