add renderer helper
This commit is contained in:
parent
510f1c81dd
commit
db8e8bfd61
18
src/helpers/renderer.js
Normal file
18
src/helpers/renderer.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import React from "react";
|
||||||
|
import { renderToString } from "react-dom/server";
|
||||||
|
import Home from "../client/components/Home";
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
const content = renderToString(<Home />);
|
||||||
|
|
||||||
|
return `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head></head>
|
||||||
|
<body>
|
||||||
|
<div id="root">${content}</div>
|
||||||
|
<script src="bundle.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`
|
||||||
|
};
|
14
src/index.js
14
src/index.js
@ -1,24 +1,14 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { renderToString } from "react-dom/server";
|
import { renderToString } from "react-dom/server";
|
||||||
|
import renderer from './helpers/renderer';
|
||||||
import Home from './client/components/Home'
|
import Home from './client/components/Home'
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.use(express.static('public'));
|
app.use(express.static('public'));
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
const content = renderToString(<Home />);
|
res.send(renderer());
|
||||||
const html = `
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head></head>
|
|
||||||
<body>
|
|
||||||
<div id="root">${content}</div>
|
|
||||||
<script src="bundle.js"></script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
`;
|
|
||||||
res.send(html);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(3000, () => {
|
app.listen(3000, () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user