You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Server-side rendering and Redux is now included!
1
+
# Behold the ReactJS + ExpressJS Boilerplate
2
+
## With Server-side rendering, Redux and React-router v4
3
3
4
-
This is a boilerplate to use ReactJS ExpressJSand Redux in a project.
4
+
This is a boilerplate to use ReactJS, ExpressJS, Redux and React Router v4 in a project.
5
5
6
6
-[ReactJS] - The state based framework for your Views
7
+
-[React Router v4] - For routing to different paths
7
8
-[Redux] - Redux manages your state
8
9
-[Babel] - The compiler to compile your JS files with es6, es7, JSX syntax to regular javascript
9
10
-[Webpack] - The module binder which takes all your JS files from different directories and compiles them into a single app.bundle.js (you can change the filename of course) so you can include it in a HTML page
@@ -12,70 +13,46 @@ This is a boilerplate to use ReactJS ExpressJS and Redux in a project.
12
13
13
14
# Installation
14
15
15
-
Node Version: v7.2.1
16
+
Node Version: v8.3.0
16
17
17
18
Just clone this repo or download the zip file. `cd` into the directory and run
18
19
19
20
npm install
20
21
21
-
To use webpack you also need to run
22
+
## Developing App with [Hot Reload]
23
+
To develop your own react application, you can take advantage of React Hot Loader and Webpack Dev Server. To develop app with hot reload:
22
24
23
-
npm install webpack -g
25
+
npm run dev
24
26
25
-
You might need to run this command as `sudo`.
27
+
Now you can access your react application on http://localhost:8080
26
28
27
-
Once you have the webpack installed, cd into the directory where you cloned this repo and run
29
+
## Production build and Deploy
30
+
To make a production build of your project, run the following commands
28
31
29
-
webpack
32
+
npm run build
33
+
34
+
This will create create two files: `index.js` in `server/public/js` and `server.js` in `server/bin`.
30
35
31
-
Note: if you would rather not install webpack globally, you can also run webpack locally:
32
-
33
-
node_modules/.bin/webpack
34
-
35
-
This will create update two files: `server.bundle.js` and `bin/app.bundle.js`.
36
-
37
-
`server.bundle.js` will be used for serving the application on port 3000 and `app.bundle.js` is the actual react app itself.
36
+
`server.js` will be used for serving the application on port 3000 and `index.js` is the actual react app itself.
38
37
39
38
Finally run
40
39
41
40
npm start
42
41
43
42
The you will be able to access this app from http://localhost:3000.
44
43
45
-
# Flow of the app/what is going on
46
-
So here is the flow.
47
-
48
-
First you create all your ReactJS files using whatever syntax you like. These files will stay in the `views` directory. If you are using Redux then you'll be using all the directories but if you don't know Redux you can just remove the `actions`, `containers` and `reducers` directories. You can use plain JSX or ES6 or Experimental ES7 in your JS files.
49
-
50
-
Once you've done that you fire up a terminal in that directory and run webpack. This has two steps:
51
-
52
-
- Webpack calls babel to compile all the files in the views directory to standard javascript, because current browsers doesn't understand ES6/ES7/JSX syntax.
53
-
- Once the compiling is done webpack takes all the compiled files and binds then into `app.bundle.js` and `server.bundle.js` along with all the dependencies.
54
-
55
-
So now you have a file in your `bin` directory which is `app.bundle.js`. This is basically your whole React app in one file. And you also have a file `server.bundle.js` which your ExpressJS server.
56
-
57
-
### Serve your app with server side rendering (and why it is important)
58
-
59
-
To serve your app with server side rendering, all you need to do is run `npm start`. The way it works is that the ExpressJS server (`server.js`) renders the app on the server and serves it to the public.
60
-
61
-
I know it's confusing. Hold on!
62
-
63
-
When the React app is served without server side rendering, a browser will download the `index.html` and load the `app.bundle.js` in real time. When the `app.bundle.js` has finished loading up it will render the page the way you have programmed. This initial render takes longer than usual and can be a nightmare for SEO or search engines. Also, a big reason of using React is how fast it is right?!
64
-
65
-
With server side rendering, the ExpressJS server will use the React engine to take your `app.bundle.js` and render the necessary parts of your webpage to plain HTML string. So the client/public will receive the same `index.html` with `app.bundle.js` included with it, but this time the rendering is already completed by the server. Your `app.bundle.js` won't modify or change anything in the page unless you have clicked something on the page.
44
+
To get a distributable tarball of your application, run this command
66
45
67
-
To understand how this server side rendering is done, you need do some research on the following resources:
46
+
npm pack
68
47
69
-
-[ReactDOMServer]
70
-
-[Redux Server Side]
71
-
-[React Router Server Side]
48
+
Remember that you have to run `npm run build` before doing this. This will create a tar.gz file in your root folder. The contents in this file is deployable. All you need to do is copy the contents inside package folder inside this tar.gz file to your server and run the app with something like [pm2].
0 commit comments