Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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