1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-18 19:19:36 +02:00

Added WebSockets with funcionality to send messages from any module

This commit is contained in:
unknown 2021-05-19 17:17:24 +02:00
parent 4510a709d4
commit 38f5d3b66a
7 changed files with 84 additions and 21 deletions

View file

@ -1,30 +1,23 @@
const express = require('express');
const http = require('http');
const { connectDB } = require('./db');
const errorHandler = require('./middleware/errorHandler');
const api = require('./api');
const jobs = require('./utils/jobs');
const colors = require('colors');
const Socket = require('./Socket');
const Sockets = require('./Sockets');
require('dotenv').config();
const PORT = process.env.PORT || 5005;
connectDB();
const app = express();
const PORT = process.env.PORT || 5005;
// Create server for Express API and WebSockets
const server = http.createServer();
server.on('request', api);
app.get('/', (req, res) => {
res.send('Server is working');
})
// Register weatherSocket
const weatherSocket = new Socket(server);
Sockets.registerSocket('weather', weatherSocket);
// Body parser
app.use(express.json());
// Link controllers with routes
app.use('/api/apps', require('./routes/apps'));
app.use('/api/config', require('./routes/config'));
app.use('/api/weather', require('./routes/weather'));
// Custom error handler
app.use(errorHandler);
app.listen(PORT, () => {
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT} in ${process.env.NODE_ENV} mode`.yellow.bold);
})