Answer the question
In order to leave comments, you need to log in
How to render in node.js - JSX (react)?
Test server code
const http = require('http');
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const StaticRouter = require('react-router').StaticRouter;
const Express = require('express');
const app = new Express();
const server = new http.Server(app);
app.set('view engine', 'ejs');
app.use('/dist', Express.static('dist'));
app.get('*', (req, res) => {
const context = {};
const html = ReactDOMServer.renderToString(
<StaticRouter location={req.url} context={context}>
<p>test</p>
</StaticRouter>
);
if (context.url) {
res.writeHead(302, {
Location: context.url
});
res.end()
} else {
res.write(html);
res.end()
}
});
server.listen(3000, (err) => {
if (err) {
return console.error(err);
}
console.info('Server running on http://localhost:3000');
});
\test-server.js:19
<StaticRouter location={req.url} context={context}>
^
SyntaxError: Unexpected token <
at new Script (vm.js:51:7)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question