Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Created April 11, 2021 19:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryanflorence/dc815490fc2bc6f5214e50de5afa1446 to your computer and use it in GitHub Desktop.
Save ryanflorence/dc815490fc2bc6f5214e50de5afa1446 to your computer and use it in GitHub Desktop.
const path = require("path");
const express = require("express");
const compression = require("compression");
const morgan = require("morgan");
const { createRequestHandler } = require("@remix-run/express");
////////////////////////////////////////////////////////////////////////////////
let app = express();
app.use(compression());
app.use(
express.static("public", {
immutable: true,
maxAge: "1y",
})
);
app.all(
"*",
process.env.NODE_ENV !== "production" ? devHandler() : prodHandler()
);
let port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Express server started on http://localhost:${port}`);
});
////////////////////////////////////////////////////////////////////////////////
function purgeAppRequireCache() {
let cwd = process.cwd();
for (let key in require.cache) {
if (key.startsWith(path.join(cwd, "build"))) {
delete require.cache[key];
}
}
}
function devHandler() {
return (req, res, next) => {
purgeAppRequireCache();
return createRequestHandler({
build: require("./build"),
getLoadContext,
})(req, res, next);
};
}
function prodHandler() {
return createRequestHandler({
build: require("./build")
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment