-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
36 lines (31 loc) · 945 Bytes
/
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
const express = require('express');
const { createServer } = require("http");
const { Server } = require("socket.io");
const { API_ENDPOINT } = require('@froyo-api/constants');
const app = express();
const httpServer = createServer(app);
require('dotenv').config();
// Routers
const apiRouter = require('./routers/api');
// Web Socket
const io = new Server(httpServer, { cors: { origin: [API_ENDPOINT] } });
io.on('connection', (socket) => {
socket.on('join-room', (room) => {
socket.join(room);
});
socket.on('send-message', async (message, room) => {
socket.to(room).emit('receive-message', message);
});
});
// App configuration
app.set('views', './views');
app.set('view engine', 'pug');
// Middleware
app.use(express.static('public'));
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({
extended: true,
limit: '50mb'
}));
app.use('/', apiRouter);
module.exports = httpServer;