-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (38 loc) · 1.23 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
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const initializeSockets = require('./sockets');
// Views setup
app.set('view engine', 'ejs');
app.set('views', __dirname + '/public/views');
// Middlewares
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.disable('x-powered-by');
app.use((req, res, next) => {
// CORS middleware
if (process.env.NODE_ENV !== 'production') {
res.set('Access-Control-Allow-Origin', '*');
} else {
res.set('Access-Control-Allow-Origin', process.env.DOMAIN_NAME);
}
// Allow service worker
res.set('Service-Worker-Allowed', '/');
next();
});
app.use('/static', express.static(`${__dirname}/public/static`));
// Websockets
initializeSockets(server);
// Routes
const { multiplayerRouter } = require('./api/routes/multiplayer');
const { pushRouter } = require('./api/routes/push');
const rootRouter = require('./routes');
app.use('/api/multiplayer', multiplayerRouter);
app.use('/api/push', pushRouter);
app.use('', rootRouter);
const PORT = process.env.PORT || 4000;
server.listen(PORT, () => console.log(`App running on ${process.env.DOMAIN_NAME || 'http://localhost'}:${PORT}`));
module.exports = app;