-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.js
More file actions
61 lines (44 loc) · 1.52 KB
/
Copy pathprogram.js
File metadata and controls
61 lines (44 loc) · 1.52 KB
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
var React = require('react');
var DOM = React.DOM;
var body = DOM.body;
var div = DOM.div;
var script = DOM.script;
var browserify = require('browserify');
var express = require('express');
var app = express();
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());
require('node-jsx').install();
var TodoBox = require('./views/index.jsx');
app.use('/bundle.js', function(req, res) {
res.setHeader('content-type', 'application/javascript');
browserify('./app.js')
.transform('reactify')
.bundle()
.pipe(res);
});
app.use('/', function(req, res) {
var initialData = JSON.stringify(data);
var markup = React.renderToString(React.createElement(TodoBox, {data: data}));
res.setHeader('Content-Type', 'text/html');
var html = React.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);
});
require('node-jsx').install();
var data = [
{title: "Shopping", detail:process.argv[3]},
{title: "Hair cut",detail:process.argv[4]}
];
app.use('/', function(req, res) {
res.render('index', {data: data});
});
app.listen(app.get('port'), function() {});