Created
February 28, 2020 13:53
-
-
Save ashour/a9518bb30b96d5405842faeab58f2094 to your computer and use it in GitHub Desktop.
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 React from "react"; | |
import express from "express"; | |
import { StaticRouter } from "react-router-dom"; | |
import { renderToString } from "react-dom/server"; | |
import App from "./common/App"; | |
import render from "./server/render"; | |
const server = express(); | |
server | |
.disable("x-powered-by") | |
.use(express.static(process.env.RAZZLE_PUBLIC_DIR)) | |
.get("/*", (req, res) => { | |
const context = {}; | |
const markup = renderToString( | |
<StaticRouter context={context} location={req.url}> | |
<App /> | |
</StaticRouter>, | |
); | |
if (context.url) { | |
res.redirect(context.url); | |
} else { | |
const html = render(markup); | |
res.status(200).send(html); | |
} | |
}); | |
export default server; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment