-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
51 lines (41 loc) · 1.21 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
const express = require('express');
const http = require('http');
const app = express();
const server = http.createServer(app);
const io = require('socket.io')(server);
const path = require('path');
const mongoose = require('mongoose');
const cors = require('cors')
require('dotenv').config();
// middleware
app.use(express.json());
app.use(cors());
// DB
mongoose.connect(process.env.DB_CONNECTION, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
var db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", function() {
console.log("DB Connection Successful");
});
// Routes
const userRoutes = require('./api/routes/users');
const roomRoutes = require('./api/routes/room');
app.use('/user', userRoutes);
app.use('/room', roomRoutes);
// Serving web page
app.use(express.static(path.join(__dirname, "./client/build")));
app.get('/*', (req, res) => {
//res.sendStatus(200);
res.sendFile(path.join(__dirname, "./client/build/index.html"));
})
// sockets
require('./api/sockets/gameSocket')(io);
// Server start
const port = process.env.PORT || 5000;
server.listen(port, ()=>{
console.log("Server listening on port "+port)
});