/
app.js
111 lines (94 loc) · 3.47 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
// Comes packaged initially
const path = require('path');
// Require for routing
const express = require('express');
// Require to interact with our database
const mongoose = require('mongoose');
// Allows us to make request to api from different domain name
const cors = require('cors');
// Parses incoming requests and grabs the data
const bodyParser = require('body-parser');
// Require for password encryption and decryption
const passport = require('passport');
// Require to know where we are getting our data from
const config = require('./config/database');
// Require for routing
const app = express();
// Require for Chat Database
const server = require('http').Server(app);
const io = require('socket.io')(server);
// =============== Routes ========================
// Requires users and buildings from routes folder
const users = require('./routes/users');
const buildings = require('./routes/buildings');
const roomInfo = require('./routes/roomInfoRoute');
const messages = require('./routes/messages');
const studyBuddy = require('./routes/studybuddies');
// =============== Set Paths ========================
// Make route public so any domain can access it
app.use(cors());
// Set static location (what is seen first)
app.use(express.static(path.join(__dirname, 'public')));
// Body Parser Middleware
app.use(bodyParser.json());
// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
// Require the config file's passport which specifies authentication
require('./config/passport')(passport);
// The routes for the specific pages
app.use('/users', users);
app.use('/buildings', buildings);
app.use('/roomInfo', roomInfo);
app.use('/messages', messages);
app.use('/studyBuddies', studyBuddy);
// Index route
app.get('/', (req, res) => {
res.send('Invalid Endpoint')
});
// Direct our homepage to be public/index.html
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
// =============== Main Database==================
// Connect to database location
mongoose.connect(config.database);
// Check to see if connected to database
mongoose.connection.on('connected', () => {
console.log('Connected to database'+config.database);
})
// Check to see if connection failed
mongoose.connection.on('error', () => {
console.log('Database not connected');
})
// =============== Chat Database ====================
io.on('connection', (socket) => {
// Say socket connected
console.log('Socket %s connected', socket.id);
// Say socket disconnected
socket.on('disconnect', () => {
console.log('Socket %s disconnected', socket.id)
});
// On emit 'join', join room
socket.on('join room', (room) => {
if (socket.room != null) {
console.log("Socket %s left room #%s", socket.id, socket.room)
socket.leave(socket.room);
}
socket.room = room;
socket.join(socket.room)
console.log("Socket %s joined room #%s", socket.id, socket.room)
})
// On emit 'add message', send to client a message
socket.on('add message', (id, sender, message) => {
io.sockets.to(id).emit('message', {'sender': sender, 'timeStamp': new Date() ,'message': message})
});
});
// =============== Listen to Ports ========================
// Port Number
// const port = 3000; // For testing
const port = process.env.PORT || 8080; // For Deployment
// Starts up chat server on separate port
server.listen(port, () => {
console.log('Server started on port '+port);
})