-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.js
72 lines (56 loc) · 1.66 KB
/
program.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const ReactDOMFactories = require('react-dom-factories');
const express = require('express');
const app = express();
const DOM = ReactDOMFactories;
const body = DOM.body;
const div = DOM.div;
const script = DOM.script;
const browserify = require('browserify');
const babelify = require("babelify");
require('babel/register');
const TodoBox = require('./views/index.jsx');
app.set('port', (process.argv[2] || 3000));
app.set('view engine', 'jsx');
app.set('views', __dirname + '/views');
app.engine('jsx', require('express-react-views').createEngine({ transformViews: false }));
require('babel/register')({
ignore: false
});
const data = [{
title: 'Shopping',
detail: process.argv[3]
}, {
title: 'Hair cut',
detail: process.argv[4]
}]
app.use('/bundle.js', function (req, res, next) {
res.setHeader('Content-Type', 'application/javascript');
browserify("./app.js")
.transform("babelify")
.bundle()
.on('error', (error) => {
console.error(error.message);
next({ status: 500 });
})
.pipe(res);
});
app.use('/', function (req, res) {
var initialData = JSON.stringify(data);
var markup = ReactDOMServer.renderToString(React.createElement(TodoBox, { data }));
res.setHeader('Content-Type', 'text/html');
var html = ReactDOMServer.renderToStaticMarkup(body(null,
div({ id: 'app', dangerouslySetInnerHTML: { __html: markup } }),
script({
id: 'initial-data',
type: 'text/plain',
'data-json': initialData
}),
script({ src: '/bundle.js' })
));
res.end(html);
});
app.listen(app.get('port'), function () {
console.log('Express server is up on port 3000');
});