Created
July 8, 2022 09:27
-
-
Save marten-cz/76c494aab539ba94a3dc974c143591ac to your computer and use it in GitHub Desktop.
React simple ssr
This file contains hidden or 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
import path from "path"; | |
import fs from "fs"; | |
import React from "react"; | |
import ReactDOMServer from "react-dom/server"; | |
import express from "express"; | |
import App from "../src/App"; | |
const PORT = process.env.PORT || 3000; | |
const app = express(); | |
app.get("/", (req, res) => { | |
fs.readFile(path.resolve("./public/index.html"), "utf8", (err, data) => { | |
if (err) { | |
console.error(err); | |
return res.status(500).send("An error occurred"); | |
} | |
return res.send( | |
data.replace( | |
'<div id="root"></div>', | |
`<div id="root">${ReactDOMServer.renderToString(<App />)}</div>` | |
) | |
); | |
}); | |
}); | |
app.use( | |
express.static(path.resolve(__dirname, ".", "dist"), { maxAge: "30d" }) | |
); | |
app.listen(PORT, () => { | |
console.log(`Server is listening on port ${PORT}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment