-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathserver.js
More file actions
28 lines (24 loc) · 723 Bytes
/
server.js
File metadata and controls
28 lines (24 loc) · 723 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import express from "express";
import path from "path";
import React from "react";
import ReactDOMServer from "react-dom/server";
import App from "./client/app.js";
function handleRender(req, res) {
const reactHtml = ReactDOMServer.renderToString(<App />);
const htmlTemplate = `<!DOCTYPE html>
<html>
<head>
<title>Universal React server bundle</title>
</head>
<body>
<div id="app">${reactHtml}</div>
<script src="public/client.bundle.js"></script>
</body>
</html>`;
res.send(htmlTemplate);
}
const app = express();
app.use("/public", express.static("./public"));
app.get("*", handleRender);
app.listen(3000);
console.log("App is running on http://localhost:3000");