-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
61 lines (54 loc) · 1.77 KB
/
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
59
60
61
/**
* dependencies
*/
var path = require('path');
var express = require('express');
var React = require('react');
var browserify = require('connect-browserify');
var app = express();
// node-jsx allow us transparently require() JSX code from node..
require('node-jsx').install({extension: '.jsx'});
var state = { count: 7 }; // initial state for the app
/**
* serve the app bundle to the client via "connect-browserify". App files will
* be watched for changes. you can edit the components and the bundle will be
* recompiled as needed, the client will always receive an updated version..
*
* @andreypopp: "You should never use this middleware in production — use nginx
* for serving pre-built bundles to a browser."
*/
app.use('/bundle.js', browserify({
entry: __dirname + '/client/app.jsx',
transforms: ['reactify'],
watch: true
}));
/**
* This middleware removes ".jsx" files from the `require` cache in a
* per-request basis. React components can be then required, re-compiled and
* re-rendered in every request which mean that you can edit ".jsx" files and
* see the changes as you go without having to restart the server.
* Use in development only.
*/
app.use(function(req, res, next){
for(var _path in require.cache){
if(path.extname(_path) == '.jsx'){
delete require.cache[_path];
}
}
next();
});
/**
* require, compile, render and send the markup..
*/
app.use(function(req, res){
// We `require` .jsx files in every request..
var ReactApp = require('./client/app.jsx');
var ReactAppMarkup = React.renderComponentToString(ReactApp(state));
res.send(
'<div id="container">' + ReactAppMarkup + '</div>' +
'<script src="/bundle.js"></script>'
);
});
app.listen(3000, function(){
console.log('server listen at localhost:3000');
});