I am trying to make a basic mongoose connection in Express using React, React Router and webpack
Are there any examples of ssr with Mongodb?
What should the server look like?
const app = express();
app.use(express.static("./build"));
app.get("*", (req, res) => {
const url = req.originalUrl || req.url;
const context = {};
const indexFile = path.resolve("./build/main.html");
if (context.url) {
req.header("Location", context.url);
return res.send(302);
}
const css = new Set();
const insertCss = (...styles) =>
styles.forEach((style) => css.add(style._getCss()));
const appContent = ReactDOMServer.renderToString(
<StyleContext.Provider value={{ insertCss }}>
<StaticRouter
location={req.url}
context={context}
></StaticRouter>
</StyleContext.Provider>
);
fs.readFile(indexFile, "utf8", (err, data) => {
if (err) {
console.log(err);
return res.status(500).send("ne rabotaet");
}
data = data.replace("__STYLES__", [...css].join(""));
data = data.replace(
"<div id=app></div>",
`<div id=app>${appContent}</div>`
);
return res.send(data);
});
});
app.listen(3000, () => {
console.log("Listenning on port: ", 3000);
});