-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
52 lines (33 loc) · 995 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var express = require('express');
var app = express();
var fs = require('fs');
var ejs = require("ejs");
var index = fs.readFileSync('index.html', 'utf-8');
//React
require('node-jsx').install({extension: '.jsx'});
var React = require('react/addons');
//Get users from database etc
var UsersData = [
{name: 'Artyom'},
{name: 'Dimitry'}
];
app.get('/', function (req, res) {
//Create React class from React factory
var UsersFactory = React.createFactory(
require('./jsx/users-react.jsx')
);
//Execute react and render result to string
var usersHTML = React.renderToString(UsersFactory({
users: UsersData
}));
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(
ejs.render(index, {serverBlock: usersHTML})
);
});
app.use(express.static(__dirname));
var server = app.listen(5000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('React app listening at http://%s:%s', host, port)
});