|
1 |
| -[](https://heroku.com/deploy) |
| 1 | +# Isomorphic React and Express |
2 | 2 |
|
3 |
| -# React Tutorial |
| 3 | +This repository shows how React can be used with the Express framework isomorphically; that is to use the same code on both the server and browser. This uses the comment box example from the [React tutorial](http://facebook.github.io/react/docs/tutorial.html) but in addition to rendering the comment box in the browser, it pre-renders it on the server, using the same code. |
4 | 4 |
|
5 |
| -This is the React comment box example from [the React tutorial](http://facebook.github.io/react/docs/tutorial.html). |
| 5 | +There are also a few other additions. I've set up `webpack` so that it bundles up the code to be used in the browser. I also didn't particularly like the `JSX` template render methods being in the same file as the controller code (despite what React says, they are controllers; they behave exactly the same way as Angular's controllers). And lastly since I had to server-side rendering, I've had to use a view engine. I've chose `Swig` as unlike `Jade` it uses proper HTML and so means I have one fewer language to learn (which fits into the philosophy of isomorphism). |
6 | 6 |
|
7 |
| -## To use |
8 |
| - |
9 |
| -There are several simple server implementations included. They all serve static files from `public/` and handle requests to `/api/comments` to fetch or add data. Start a server with one of the following: |
10 |
| - |
11 |
| -### Node |
12 |
| - |
13 |
| -```sh |
14 |
| -npm install |
15 |
| -node server.js |
16 |
| -``` |
| 7 | +Naturally this means all of the other server language implementations have been removed - Python, PHP etc. |
17 | 8 |
|
18 |
| -### Python |
| 9 | +## Implementation |
19 | 10 |
|
20 |
| -```sh |
21 |
| -pip install -r requirements.txt |
22 |
| -python server.py |
23 |
| -``` |
| 11 | +The example file from the tutorial is now in a publicly-inaccessible location at `src/example.js`. The templates are in another file which is `require`d from there, `templates.jsx`. |
24 | 12 |
|
25 |
| -### Ruby |
26 |
| -```sh |
27 |
| -ruby server.rb |
28 |
| -``` |
| 13 | +In the browser, the main entry point method which calls `ReactDOM.render`, is exported as `render` from `example.js`. This is made accessible as a window method in `src/index.js`, which is used as the entry script for `webpack`. This is all compiled into `public/scripts/bundle.js` which is what the browser includes. The settings for the `webpack` are found in `webpack.config.js`; several libraries like React itself and the markdown parser are set to be externals, which means they are not bundled up so that the browser can load those from a CDN (Content Delivery Network). |
29 | 14 |
|
30 |
| -### PHP |
31 |
| -```sh |
32 |
| -php server.php |
33 |
| -``` |
| 15 | +On the browse, `example.js` itself is `require`d, after the `node-jsx` module is set up, in order that the JSX syntax can be understood. `example.js` also exports a `renderServer` which returns a static string after calling `ReactDOMServer.renderToString` from the `react-dom/server` module. The route for `/` simply calls this method and passes it as a variable to the `views/index.html` view. |
34 | 16 |
|
35 |
| -### Go |
36 |
| -```sh |
37 |
| -go run server.go |
38 |
| -``` |
39 |
| - |
40 |
| -### Perl |
41 |
| - |
42 |
| -```sh |
43 |
| -cpan Mojolicious |
44 |
| -perl server.pl |
45 |
| -``` |
| 17 | +## To use |
46 | 18 |
|
47 |
| -And visit <http://localhost:3000/>. Try opening multiple tabs! |
| 19 | + npm install |
| 20 | + |
| 21 | +Next, to compile the browser code into `public/scripts/bundle.js`: |
48 | 22 |
|
49 |
| -## Changing the port |
| 23 | + npm run webpack-dev |
| 24 | + |
| 25 | +Or if you want to compile it to a minified version without the source map: |
50 | 26 |
|
51 |
| -You can change the port number by setting the `$PORT` environment variable before invoking any of the scripts above, e.g., |
| 27 | + npm run webpack |
| 28 | + |
| 29 | +And then start the server: |
52 | 30 |
|
53 |
| -```sh |
54 |
| -PORT=3001 node server.js |
55 |
| -``` |
| 31 | + npm start |
| 32 | + |
| 33 | +And visit <http://localhost:3000/>. |
0 commit comments