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 React from 'react';
import { renderToString } from "react-dom/server";
import renderer from './helpers/renderer';
import Home from './client/components/Home'
const app = express();
app.use(express.static('public'));
app.get('/', (req, res) => {
const content = renderToString(<Home />);
const html = `
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="root">${content}</div>
<script src="bundle.js"></script>
</body>
</html>
`;
res.send(html);
res.send(renderer());
});
app.listen(3000, () => {