-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (56 loc) · 1.62 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
const express = require('express')
require('express-async-errors')
const connectDB = require('./dbconnectiion/index')
const authRouter = require('./routes/authRouter')
const messageRouter = require('./routes/messagesRouter')
const searchRouter = require('./routes/usersearch')
const authenticateUser = require('./middleware/authentication')
const app = express()
const server = require('http').createServer(app)
const cors = require('cors')
const dotenv = require('dotenv')
dotenv.config()
app.use(
cors({
origin: '*',
})
)
const notFoundMiddleware = require('./middleware/notfound')
const errorHandlerMiddlewarec = require('./middleware/errors')
const DB_CONNECTION_URL = process.env.API_KEY
const PORT = process.env.PORT || 3333
// socket
const io = require('socket.io')(server, {
cors: {
origin: ['http://localhost:3000','https://talkease.netlify.app'
],
},
})
io.sockets.on('connection', function (socket) {
socket.on('join', ({ name }) => {
socket.join(name)
})
socket.on('sendmsg', (data, name) => {
socket.to(name).emit('receive-msg', data)
})
// console.log('connected')
})
// io.sockets.on('sendmsg', (data) => {
// console.log(data.msg)
// })
app.use(express.json())
//routes
app.use('/api/v1/auth', authRouter)
app.use('/api/v1/chats', authenticateUser, messageRouter)
app.use('/api/v1/users', authenticateUser, searchRouter)
app.use(notFoundMiddleware)
app.use(errorHandlerMiddlewarec)
const startServer = async () => {
try {
await connectDB(DB_CONNECTION_URL)
server.listen(PORT, console.log(`server is lesting at port ${PORT}`))
} catch (error) {
console.log(error)
}
}
startServer()