← React Beginner Questions - Set 5 Q44
React Beginner Questions - Set 5
The HTML is produced on the server so the first paint contains content; React then takes over in the browser.
// server.js (Express + React 18)
import express from 'express';
import { renderToString } from 'react-dom/server';
import App from './App';
const app = express();
app.get('*', (req, res) => {
const html = renderToString(<App url={req.url} />);
res.send(`<!doctype html>
<html><body>
<div id="root">${html}</div>
<script src="/client.js"></script>
</body></html>`);
});
app.listen(3000);