-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (33 loc) · 1 KB
/
Copy pathserver.js
File metadata and controls
41 lines (33 loc) · 1 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
const express= require('express');
const http= require("http");
const mongoose= require('mongoose');
const path= require('path');
const app= express();
const server= http.createServer(app);
const io= require('socket.io')(server);
const {saveHighScore, sendHighest} = require("./controller/scoreController");
const PORT= process.env.PORT || 3000;
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/snakedb",
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false
}
)
app.use(express.static(path.join(__dirname, "public")));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, "./public/index.html"));
});
io.on("connection", socket => {
console.log(socket.id);
sendHighest(res => {
socket.emit('highscores', res);
})
socket.on("score", data => {
if (data.name !== null) {
saveHighScore(data);
}
})
})
server.listen(PORT, () => console.log(`server listening on PORT: ${PORT}`));