-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
70 lines (63 loc) · 1.96 KB
/
server.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// require packages
require('dotenv').config()
const express = require('express')
const cors = require('cors')
const rowdy = require('rowdy-logger')
const app = express()
// requiring things needed to incorporate socketIo
const { createServer } = require('http')
const socketIo = require('socket.io')
const server = createServer(app)
const io = socketIo(server,{
cors: {
// linking to react app
origin: '*'
//might need to include METHODS
}
})
// config express app
const PORT = process.env.PORT || 8000
// for debug logging
const rowdyResults = rowdy.begin(app)
// cross origin resource sharing
app.use(cors())
// request body parsing
app.use(express.json())
app.use((req,res,next)=>{
req.io = io
return next()
})
const myMiddleware = (req, res, next) => {
// I am a middleware
console.log('Hi 👋 the middleware has been invoked!')
next() // makes express move on to the next route/middleware
}
io.on('connection',(socket)=>{
//console.log(socket.id)
// on render in react the user is sent here to join that specific room they're in so they see only the content there
socket.on('join-chat',(chatId)=>{
socket.join(chatId)
console.log(chatId,"room hoined")
})
// when users on react decide to send a message payload it goes here and will emit that message to everyone else in that same room as them
socket.on('send-comment',(message)=>{
console.log(message,'here')
console.log(message.comment)
//socket.join(message.room)
socket.to(`${message.room}`).emit('receive-comment',message)
})
})
// app.use(myMiddleware)
// GET / -- test index route
// defining a function as route specific middleware
app.get('/', myMiddleware, (req, res) => {
res.json({ msg: 'hello backend 🤖' })
})
// controllers
app.use('/users', require('./controllers/api-v1/users.js'))
app.use('/chats', require('./controllers/chats.js'))
// hey listen
server.listen(PORT, () => {
rowdyResults.print()
console.log(`is that port ${PORT} I hear? 🙉`)
})