Answer the question
In order to leave comments, you need to log in
How to add script to jsx code?
I'm starting to write an application in Express with pre-rendering on the server.
Sketched app.js:
var http = require('http');
var express = require('express');
var React = require('react');
var ReactDOMServer = require('react-dom/server');
//component with basic layout
var DefaultHtml = require('./src/components/default');
var bodyParser = require('body-parser');
var config = require('./config');
var app = express();
// Using bodyParser middleware
app.use( bodyParser.json() );
http.createServer(app).listen(config.serverPort);
app.get('/', function (req, res) {
var html = ReactDOMServer.renderToString(<DefaultHtml />);
console.log(html);
res.status(200);
res.send(`<!doctype html>${html}`)
});
class DefaultHtml extends React.Component {
render() {
return <html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
}
}
module.exports = DefaultHtml;
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