forked from markfinger/python-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_server.js
58 lines (50 loc) · 1.17 KB
/
render_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
53
54
55
56
57
58
var argv = require('yargs')
.option('p', {
alias: 'port',
description: 'Specify the server\'s port',
default: 9009
})
.option('a', {
alias: 'address',
description: 'Specify the server\'s address',
default: '127.0.0.1'
})
.help('h').alias('h', 'help')
.strict()
.argv;
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var reactRender = require('react-render');
// Ensure support for loading files that contain ES6+7 & JSX
require('babel-core/register');
var ADDRESS = argv.address;
var PORT = argv.port;
var app = express();
var server = http.Server(app);
app.use(bodyParser.json());
app.get('/', function(req, res) {
res.end('React render server');
});
app.post('/render', function(req, res) {
reactRender(req.body, function(err, markup) {
if (err) {
res.json({
error: {
type: err.constructor.name,
message: err.message,
stack: err.stack
},
markup: null
});
} else {
res.json({
error: null,
markup: markup
});
}
});
});
server.listen(PORT, ADDRESS, function() {
console.log('React render server listening at http://' + ADDRESS + ':' + PORT);
});