-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (45 loc) · 1.56 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
const fs = require("fs");
const { createServer } = require("http");
const path = require("path");
const { createRequestHandler } = require("@remix-run/express");
const compression = require("compression");
const express = require("express");
const morgan = require("morgan");
const MODE = process.env.NODE_ENV;
const BUILD_DIR = path.join(process.cwd(), "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);
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.all(
"*",
MODE === "production"
? createRequestHandler({ build: require("./build") })
: (req, res, next) => {
purgeRequireCache();
const build = require("./build");
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() {
for (const key in require.cache) {
if (key.startsWith(BUILD_DIR)) {
delete require.cache[key];
}
}
}