-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
126 lines (85 loc) · 2.42 KB
/
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const express = require('express')
const app = express()
const mongoose = require('mongoose')
const PORT = process.env.PORT || 5000
const fileUpload = require('express-fileupload');
require("dotenv").config()
mongoose.connect(process.env.MONGOURI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
mongoose.connection.on('connected', () => {
console.log("Connected to mongodb !!")
})
mongoose.connection.on('error', (err) => {
console.log("err connecting", err)
})
require('./models/user')
require('./models/post')
require('./models/chatroom')
require('./models/message')
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(fileUpload({
useTempFiles : true,
tempFileDir : '/tmp/'
}));
app.use(require('./routes/auth'))
app.use(require('./routes/post'))
app.use(require('./routes/user'))
app.use(require('./routes/chat'))
if(process.env.NODE_ENV === "production"){
app.use(express.static('client/build'))
const path = require('path')
app.get("*",(req,res)=>{
res.sendFile(path.resolve(__dirname,'client','build','index.html'))
})
}
const server = app.listen(PORT, () => {
console.log("Server is running on", PORT)
})
const io = require('socket.io')(server)
const jwt = require('jsonwebtoken')
const Message = mongoose.model("Message")
const User = mongoose.model("User")
io.use(async (socket, next) => {
const token = socket.handshake.query.token;
jwt.verify(token, process.env.JWT_SECRET, (err, payload) => {
if (err) {
return new Error('authentication error');
}
const { _id } = payload
socket.userId = _id;
})
next();
})
io.on('connection', (socket) => {
console.log("Connected" + socket.userId)
socket.on("disconnect", () => {
console.log("Disconnected: " + socket.userId);
})
socket.on("joinRoom", ({ chatroomId }) => {
socket.join(chatroomId)
console.log("A user joined chatroom " + chatroomId)
})
socket.on("leaveRoom", ({ chatroomId }) => {
socket.join(chatroomId)
console.log("A user left chatroom " + chatroomId)
})
socket.on("chatroomMessage", async ({ chatroomId, message }) => {
if (message.trim().length > 0) {
const user = await User.findOne({ _id: socket.userId })
const newMessage = new Message({
chatroom: chatroomId,
user: socket.userId,
message
})
io.to(chatroomId).emit("newMessage", {
message,
name: user.name,
userId: socket.userId
})
await newMessage.save()
}
})
})