Skip to content

Commit d89f7c5

Browse files
committed
Added isomorphism, swig views, webpack configuration and separated JSX templates into separate file
1 parent 7b675c8 commit d89f7c5

17 files changed

+323
-496
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*~
22
node_modules
33
.DS_Store
4+
.idea
5+
npm-debug.log

README.md

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,33 @@
1-
[![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy)
1+
# Isomorphic React and Express
22

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.
44

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).
66

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.
178

18-
### Python
9+
## Implementation
1910

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`.
2412

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).
2914

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.
3416

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
4618

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`:
4822

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:
5026

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:
5230

53-
```sh
54-
PORT=3001 node server.js
55-
```
31+
npm start
32+
33+
And visit <http://localhost:3000/>.

package.json

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,26 @@
55
"main": "server.js",
66
"dependencies": {
77
"body-parser": "^1.4.3",
8-
"express": "^4.4.5"
8+
"express": "^4.4.5",
9+
"marked": "^0.3.5",
10+
"node-jsx": "^0.13.3",
11+
"react": "^0.14.7",
12+
"react-dom": "^0.14.7",
13+
"swig": "^1.4.2"
14+
},
15+
"devDependencies": {
16+
"babel-core": "^6.4.5",
17+
"babel-loader": "^6.2.2",
18+
"babel-preset-es2015": "^6.3.13",
19+
"babel-preset-react": "^6.3.13",
20+
"babelify": "^7.2.0",
21+
"webpack": "^1.12.13"
922
},
10-
"devDependencies": {},
1123
"scripts": {
1224
"test": "echo \"Error: no test specified\" && exit 1",
13-
"start": "node server.js"
25+
"start": "node server.js",
26+
"webpack-dev": "webpack --watch --dev",
27+
"webpack": "webpack"
1428
},
1529
"repository": {
1630
"type": "git",
@@ -27,7 +41,7 @@
2741
"url": "https://github.com/reactjs/react-tutorial/issues"
2842
},
2943
"homepage": "https://github.com/reactjs/react-tutorial",
30-
"engines" : {
31-
"node" : "0.12.x"
44+
"engines": {
45+
"node": "0.12.x"
3246
}
3347
}

public/scripts/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

public/scripts/example.js

Lines changed: 0 additions & 146 deletions
This file was deleted.

requirements.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

server.go

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)