-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.js
56 lines (48 loc) · 1.74 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
// npm packages required / dependencies
const express = require("express");
const bodyParser = require("body-parser");
const routes = require("./routes")
// sets up the Express App
const app = require("express")();
// sets the environment variable PORT to tell the web server what port to listen on
const PORT = process.env.PORT || 3001;
// serves up static assets (usually on heroku)
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// configures body parser for AJAX requests and
// sets up the Express app to handle data parsing
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(bodyParser.json({ type: "application/vnd.api+json" }));
// adds routes, both API and view
app.use(routes);
// requires models for syncing
var db = require("./models")
// integrates socket.io
const http = require('http').Server(app);
// initializes a new instance of socket.io
// by passing the http (the HTTP server) object
const io = require('socket.io')(http);
// listens on the connection event for incoming sockets,
// and logs it to the console.
io.on('connection', function (socket) {
console.log('a user connected');
// listens on 'checkIn' event
socket.on('checkIn', function (id) {
console.log('checked in into workshop: ' + id);
// sends id to everyone except for a certain socket
// uses broadcast flag
socket.broadcast.emit('checkedIn', id);
});
// socket.on('disconnect', function () {
// console.log('Got disconnect!');
// });
});
// syncs the sequelize models and then connects the Express app
db.sequelize.sync().then(function () {
http.listen(PORT, function () {
console.log(`🌎 ==> Server now on port ${PORT}!`);
});
});