-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
33 lines (27 loc) · 859 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
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
require('./libs/db-connection');
app.use('/public', express.static('public'));
app.set('view engine', 'ejs');
const Chat = require('./models/Chat');
app.get('/', (req, res) => {
Chat.find({}).then(messages => {
res.render('index', {messages});
}).catch(err => console.error(err));
});
io.on('connection', socket => {
socket.on('chat', data => {
Chat.create({name: data.handle, message: data.message}).then(() => {
io.sockets.emit('chat', data); // return data
}).catch(err => console.error(err));
});
socket.on('typing', data => {
socket.broadcast.emit('typing', data); // return data
});
});
// listen
http.listen(process.env.PORT || 3000, () => {
console.log('Running');
});