-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
105 lines (88 loc) · 2.92 KB
/
server.js
File metadata and controls
105 lines (88 loc) · 2.92 KB
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
// require necessary NPM packages
const express = require('express')
const mongoose = require('mongoose')
const cors = require('cors')
// require route files
const chatRoutes = require('./app/routes/chat_routes')
const userRoutes = require('./app/routes/user_routes')
const profileRoutes = require('./app/routes/profile_routes')
// require middleware
const errorHandler = require('./lib/error_handler')
const requestLogger = require('./lib/request_logger')
// require database configuration logic
// `db` will be the actual Mongo URI as a string
const db = require('./config/db')
// require configured passport authentication middleware
const auth = require('./lib/auth')
// define server and client ports
// used for cors and local port declaration
const serverDevPort = 4741
const clientDevPort = 7165
// establish database connection
// use new version of URL parser
// use createIndex instead of deprecated ensureIndex
mongoose.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
})
// instantiate express application object
const app = express()
const http = require('http')
const server = http.createServer(app)
const socketIo = require('socket.io')
// server is allowing requests from client
const io = socketIo(server, {
cors: {
origin: 'https://mosaco-group.github.io',
methods: ['GET', 'POST']
}
})
app.use((req, res, next) => {
req.io = io
next()
})
// on connecting
io.on('connection', (socket) => {
// socket is listening for 'client message'
socket.on('client message', (data) => {
// sending message to everyone, including the sender
socket.broadcast.emit('server message', data)
socket.emit('server message', data)
// disconnecting from Socket
socket.on('disconnect', () => {
})
})
})
// set CORS headers on response from this API using the `cors` NPM package
// `CLIENT_ORIGIN` is an environment variable that will be set on Heroku
app.use(
cors({
origin: process.env.CLIENT_ORIGIN || `http://localhost:${clientDevPort}`
})
)
// define port for API to run on
const port = process.env.PORT || serverDevPort
// register passport authentication middleware
app.use(auth)
// add `express.json` middleware which will parse JSON requests into
// JS objects before they reach the route files.
// The method `.use` sets up middleware for the Express application
app.use(express.json())
// this parses requests sent by `$.ajax`, which use a different content type
app.use(express.urlencoded({ extended: true }))
// log each request as it comes in for debugging
app.use(requestLogger)
// register route files
app.use(chatRoutes)
app.use(userRoutes)
app.use(profileRoutes)
// register error handling middleware
// note that this comes after the route middleware, because it needs to be
// passed any error messages from them
app.use(errorHandler)
// run API on designated port (4741 in this case)
server.listen(port, () => {
})
// needed for testing
module.exports = app