add renderer helper

This commit is contained in:
agatha 2024-04-12 20:09:51 -04:00
parent 510f1c81dd
commit db8e8bfd61
2 changed files with 20 additions and 12 deletions

18
src/helpers/renderer.js Normal file
View 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>
`
};

View File

@ -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, () => {