-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
78 lines (59 loc) · 2.26 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require('dotenv').config();
const http = require('http');
const express = require('express');
const path = require('path');
const notes = require('./lib/routes/notes');
const statistics = require('./lib/routes/statistics');
const { initDatabase, createIndexes } = require('./lib/database/database');
const { initSocket } = require('./src/socket');
const port = process.env.PORT || 8080;
const socketPort = process.env.SOCKET_PORT || 4000;
const app = express();
// Parse application/json for all request
app.use(express.json());
// Setup api endpoints
app.use('/api/notes', notes);
app.use('/api/statistics', statistics);
if (process.env.NODE_ENV === 'production') {
// Web microphone socket
const server = http.createServer(app);
initSocket(server);
// Serve any static files
app.use(express.static(path.join(__dirname, 'client/build')));
app.use(express.static(path.join(__dirname, 'client/storybook-static')));
// Handle React routing, return all requests to React app
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
// Setup Handle React routing, return all requests to React app
app.get('/storybook', (req, res) => {
res.sendFile(path.join(__dirname, 'client/storybook-static', 'index.html'));
});
initDatabase(process.env.DB_URL, process.env.DB_NAME).then(async () => {
console.log(`Database ${process.env.DB_NAME} is ready`);
await createIndexes();
console.log('Indexes created');
server.listen(port, () =>
console.log(`Express server app listening at http://localhost:${port}`)
);
});
} /* development */ else {
// Web microphone socket
const socket = http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('SocketIO');
res.end();
});
socket.listen(socketPort, 'localhost', () => {
console.log(`SocketIO listening at http://localhost:${socketPort}`);
});
initSocket(socket);
initDatabase(process.env.DB_URL, process.env.DB_NAME).then(async () => {
console.log(`Database ${process.env.DB_NAME} is ready`);
await createIndexes();
console.log('Indexes created');
app.listen(port, () =>
console.log(`Express server app listening at http://localhost:${port}`)
);
});
}