Created
April 11, 2021 19:42
-
-
Save ryanflorence/dc815490fc2bc6f5214e50de5afa1446 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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