-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
45 lines (41 loc) · 1.32 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
const express = require("express");
const http = require("http");
var livereload = require("livereload");
var connectLiveReload = require("connect-livereload");
const { initializeWebsocketServer } = require("./server/websocketserver");
const { initializeAPI } = require("./server/api");
// Create the express server
const app = express();
const server = http.createServer(app);
// create a livereload server
const env = process.env.NODE_ENV || "development";
if (env !== "production") {
const liveReloadServer = livereload.createServer();
liveReloadServer.server.once("connection", () => {
setTimeout(() => {
liveReloadServer.refresh("/");
}, 100);
});
// use livereload middleware
app.use(connectLiveReload());
}
// deliver static files from the client folder like css, js, images
app.use(express.static("client"));
// route for the homepage
app.get("/", (req, res) => {
res.sendFile(__dirname + "/client/index.html");
});
// Allowing top-level await
(async function () {
// Initialize the websocket server
await initializeWebsocketServer(server);
// Initialize the REST api
initializeAPI(app);
//start the web server
const serverPort = process.env.PORT || 3000;
server.listen(serverPort, () => {
console.log(
`Express Server started on port ${serverPort} as '${env}' Environment`
);
});
})();