Created
August 6, 2019 16:19
-
-
Save ovpv/6f6f42fc2a83394230f5786c26348aaf to your computer and use it in GitHub Desktop.
Serverside rendered react app
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
import express from "express"; | |
import React from "react"; | |
import { renderToString } from "react-dom/server"; | |
import App from "./client/app"; | |
const app = express(); | |
const port = 3000; | |
const HTML = (req, context) => { | |
const body = renderToString(<App />); | |
return `<html> | |
<head> | |
<title>React basic SSR </title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
</head> | |
<body style="margin:0;"> | |
<div id="app"> | |
${body} | |
</div> | |
<script src="client.js"></script> | |
</body> | |
</html>`; | |
}; | |
const context = {}; | |
app.use(express.static("dist")); | |
app.get("/", (req, res) => { | |
return res.send(HTML(req, context)); | |
}); | |
app.listen(port, () => console.log(`Example app listening on port ${port}!`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment