react-dom/server#renderToNodeStream JavaScript Examples

The following examples show how to use react-dom/server#renderToNodeStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.js    From citr-v6-project with Apache License 2.0 6 votes vote down vote up
app.use((req, res) => {
  res.write(parts[0]);
  const staticContext = {};
  const reactMarkup = (
    <StaticRouter url={req.url} context={staticContext}>
      <App />
    </StaticRouter>
  );

  const stream = renderToNodeStream(reactMarkup);
  stream.pipe(res, { end: false });
  stream.on("end", () => {
    res.status(staticContext.statusCode || 200);
    res.write(parts[1]);
    res.end();
  });
});
Example #2
Source File: index.js    From snowpack-react-ssr with MIT License 6 votes vote down vote up
app = (req, res) => {
  if (
    req.url.endsWith('.js') ||
    req.url.endsWith('.map') ||
    req.url.endsWith('.css')
  ) {
    staticServer(req, res);
  } else {
    res.write(`
        <html>
            <head>
                <link rel="canonical" url="${req.url}">
                <style>
                  ${criticalCSS.toString()}
                </style>
                ${development ? SNOWPACK_HMR_SCRIPT : preloadTags}
            </head>
            <body><div id="root">`);
    const node = renderToNodeStream(<AppComponent />);
    node.pipe(res, { end: false });
    node.on('end', () => {
      res.write(
        development
          ? `</div></body>${SNOWPACK_DEV_SCRIPT}</html>`
          : `</div></body>${scriptTags}</html>`,
      );
      res.end();
    });
  }
}