-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
45 lines (35 loc) · 1.18 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
const express = require('express')
const cookieParser = require('cookie-parser')
const cors = require('cors')
const morgan = require('morgan')
const dotenv = require('dotenv')
const connectDB = require('./config/db')
const SocketServer = require('./SocketServer')
const path = require('path')
const { ExpressPeerServer } = require('peer')
dotenv.config({
path: './config/.env'
})
const app = express()
const http = require('http').createServer(app)
const io = require('socket.io')(http)
io.on('connection', socket => {
SocketServer(socket)
})
ExpressPeerServer(http, { path: '/' })
app.use(express.json())
app.use(express.urlencoded({extended: true}))
app.use(cors())
app.use(cookieParser())
app.use(morgan('dev'))
app.use('/api/v1/auth', require('./routes/auth.route'))
app.use('/api/v1/user', require('./routes/user.route'))
app.use('/api/v1/message', require('./routes/message.route'))
connectDB()
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'))
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'))
})
}
http.listen(process.env.PORT, () => console.log(`Server is running on PORT ${process.env.PORT}.`))