-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
150 lines (122 loc) · 4.57 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// const path = require("path");
// const express = require("express");
// const compression = require("compression");
// const morgan = require("morgan");
// const { createRequestHandler } = require("@remix-run/express");
// const BUILD_DIR = path.join(process.cwd(), "build");
// const app = express();
// app.use(compression());
// // http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header
// app.disable("x-powered-by");
// // Remix fingerprints its assets so we can cache forever.
// app.use(
// "/build",
// express.static("public/build", { immutable: true, maxAge: "1y" })
// );
// // Everything else (like favicon.ico) is cached for an hour. You may want to be
// // more aggressive with this caching.
// app.use(express.static("public", { maxAge: "1h" }));
// app.use(morgan("tiny"));
// app.all(
// "*",
// process.env.NODE_ENV === "development"
// ? (req, res, next) => {
// purgeRequireCache();
// return createRequestHandler({
// build: require(BUILD_DIR),
// mode: process.env.NODE_ENV,
// })(req, res, next);
// }
// : createRequestHandler({
// build: require(BUILD_DIR),
// mode: process.env.NODE_ENV,
// })
// );
// const port = process.env.PORT || 3000;
// app.listen(port, () => {
// console.log(`Express server listening on port ${port}`);
// });
// function purgeRequireCache() {
// // purge require cache on requests for "server side HMR" this won't let
// // you have in-memory objects between requests in development,
// // alternatively you can set up nodemon/pm2-dev to restart the server on
// // file changes, but then you'll have to reconnect to databases/etc on each
// // change. We prefer the DX of this, so we've included it for you by default
// for (let key in require.cache) {
// if (key.startsWith(BUILD_DIR)) {
// delete require.cache[key];
// }
// }
// }
const path = require("path");
const express = require("express");
var bodyParser = require("body-parser");
const { createServer } = require("http");
const { Server } = require("socket.io");
const compression = require("compression");
const morgan = require("morgan");
const fs = require("fs");
const { createRequestHandler } = require("@remix-run/express");
const MODE = process.env.NODE_ENV;
const BUILD_DIR = path.join(process.cwd(), "server/build");
if (!fs.existsSync(BUILD_DIR)) {
console.warn(
"Build directory doesn't exist, please run `npm run dev` or `npm run build` before starting the server."
);
}
const app = express();
// You need to create the HTTP server from the Express app
const httpServer = createServer(app);
// And then attach the socket.io server to the HTTP server
const io = new Server(httpServer);
// Then you can use `io` to listen the `connection` event and get a socket
// from a client
io.on("connection", (socket) => {
// from this point you are on the WS connection with a specific client
console.log(socket.id, "connected");
socket.emit("confirmation", "connected!");
socket.on("event", (data) => {
console.log(socket.id, data);
// socket.emit("event", "pong");
});
});
app.use(compression());
// You may want to be more aggressive with this caching
app.use(express.static("public", { maxAge: "1h" }));
// Remix fingerprints its assets so we can cache forever
app.use(express.static("public/build", { immutable: true, maxAge: "1y" }));
app.use(morgan("tiny"));
app.use(bodyParser.json());
app.post("/test", (req, res) => {
io.emit("test", req.body);
res.end();
});
app.get(
"*",
MODE === "production"
? createRequestHandler({ build: require("./build") })
: (req, res, next) => {
purgeRequireCache();
const build = require("./build");
io.emit("test", "TEEST");
return createRequestHandler({ build, mode: MODE })(req, res, next);
}
);
const port = process.env.PORT || 3000;
// instead of running listen on the Express app, do it on the HTTP server
httpServer.listen(port, () => {
console.log(`Express server listening on port ${port}`);
});
////////////////////////////////////////////////////////////////////////////////
function purgeRequireCache() {
// purge require cache on requests for "server side HMR" this won't let
// you have in-memory objects between requests in development,
// alternatively you can set up nodemon/pm2-dev to restart the server on
// file changes, we prefer the DX of this though, so we've included it
// for you by default
for (const key in require.cache) {
if (key.startsWith(BUILD_DIR)) {
delete require.cache[key];
}
}
}