Essentially, you need to configure the node.js server, which for each request can respond to the result of rendering the response component as a regular string. Key React.renderToString . An example combined with react-router :
import express from "express"; import React from "react"; import Router from "react-router"; const app = express(); // set up Jade app.set('views', './views'); app.set('view engine', 'jade'); import routes from "../shared/routes"; app.get('/*', function (req, res) { Router.run(routes, req.url, Handler => { let content = React.renderToString(<Handler />); res.render('index', { content: content }); }); }); var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); });
React-router is useful for loading url-based components, but this is not strictly necessary. In any case, if you are new to ecosystem response, I suggest you take a look at this starter kit for isomorphic responsive applications. How you should know what you are trying to do is called isomorphic Javascript.
Davide oungari
source share