Boilerplate for React application with webpack using alt's Flux running on a node express server.
| React + Redux / alt + Immutable + Express + mongoose + MongoDB |
https://react-webpack-node.herokuapp.com/
Isomorphic Flux using:
On master branch:
On flux/alt branch:
- alt as my Flux implementation
- iso to help with bootstrapping data for isomorphic Flux
- react-router
Note: If you have previously used an alt implementation of this repository, please refer to this branch. I will not be updating it as frequently as master, but definitely welcome suggestions!
- Stores storing data using ImmutableJS data structures - currently only on flux/alt
- Css Modules
- Webpack config file
- Express server
- Mongoose for MongoDB
- Procfile to enable deployment to Heroku.
I'm really a fan of this implementation. The main principles of having:
- a single store
- state being read-only (you have to express an intent to mutate being creating actions)
- mutations are written as pure functions
make it very fun and easy to write predictable code! Redux also has a really good ecosystem and strong support from the community.
Having isomorphic React was one of my key criteria when choosing a Flux library, which helped narrow down the scope of libraries.
I found alt's implementation to be clean and simple, and like the option of allowing us to create alt instances or using singletons (and flushing the stores). I also like the direction in which alt is heading.
The aim of this repo is to incorporate the best practices to building a non-trivial apps with Reactjs and Node. I am working to document this repo extensively so it would be easy for both beginners and experts to begin dev-ing on it without pulling your hair out.
npm install
npm run dev
to run locally
There are 3 different "modes" you can develop in:
- Development - without react-hot-loader
- Development - with react-hot-loader
- Production
npm run watch
watches and recompiles on file changesnpm run dev
will run the server locally without a proxy. The difference betweendev
andnpm start
is thatnpm start
requires you to access your site over HTTPS, otherwise session cookies will not be set.
### Development build with Hot Loader
We use react-hot-loader, which is about the greatest thing that has ever happened. No browser refreshes needed.
1. npm run devHotLoader
to build with webpack and start the server. We use webpack-dev-server as a proxy server to serve assets. Changes made are not saved to disk, as that is not what webpack-dev-server is for. However, npm run watchHotLoader
IF you want to reload the page and see the change in the server-rendered React.
Currently I'm working on using React Transform HMR to replace hot loader and hopefully make dev work easier.
Run the commands below for a production build, i.e. what is deployed to Heroku. If you are deploying to Heroku or similar, we assume that you serving the pages over HTTPS.
npm run build && npm start
npm run build
runswebpack
will run configurations within webpack.config.js.npm run watch
runswebpack --watch
to watch and recompile for changes.
We use ExtractTextPlugin to extract compiled css in our webpack config file
babel-loader. Seriously, try it!
Install MongoDB:
brew update
brew install mongodb
mongod
(Make sure you have the permissions to the directory /data/db)
If you're interested in a boilerplate example with postgresql, check reap out!
heroku create
heroku app:rename newname
if you need togit push heroku master
Note: If you are working from a different machine and get heroku does not appear to be a remote repository
message, be sure to run git remote add heroku git@heroku.com:appname.git
.
heroku open
to open the link- If you wish to have a database setup on Heroku, remember to use
heroku addons:add mongohq
orheroku addons:add mongolab
.
Note: For Google Auth, read Setting up Google Authentication below
- Create a Droplet
- Follow this or this tutorial to set up nodejs
- Follow this tutorial to install mongodb
- git clone this repo
npm install
sudo npm install pm2 -g
pm2 start server/index.js
pm2 startup ubuntu
sudo env PATH=$PATH:/usr/local/bin pm2 startup ubuntu -u sammy
Read more on DO config here
- app.js
- App.react
- NavigationBar.react
- RouteHandler - Vote.react - EntryBox.react - MainSection.react - Scoreboard.react - Login.react - Logout.react - About.react
- App.react
- Follow these steps from Google to create your API keys on Google Developers Console
- Under APIs & Auth, Copy your Client ID and Client Secret
For Google Auth to work locally, you need to do the following in your terminal before starting the server:
export GOOGLE_CLIENTID=YOUR_CLIENTID
export GOOGLE_SECRET=YOUR_SECRET
Fret not! Heroku's covered this pretty well.
heroku config:set GOOGLE_CLIENTID=YOUR_CLIENTID
heroku config:set GOOGLE_SECRET=YOUR_SECRET
heroku config:set GOOGLE_CALLBACK=YOUR_CALLBACK
Don't you worry child. Read this.
You can learn more about ES6 (or ES2015) here.
3. Why do I get Error: Failed to serialize user into session
when trying to login with email/password locally?
It's because there are no users created in your local DB so it's throwing an error on the server's end. We haven't set up the handling of errors for this yet. I intend to fix this. If you check this, you'll see that there is a /signup
endpoint for creating a user. In the meantime, a quick and easy way to do this is to paste this in your console log while your server is running:
var http = new XMLHttpRequest();
var url = "http://localhost:3000/signup";
var params = "email=example@ninja.com&password=ninja";
http.open("POST", url, true);http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(params);
This should create a user in your local database and all will be well!!
This is because we do not use ExtractTextPlugin in our dev config. It won't happen in production because we extract and serve the css as a separate file. It's something we can all live with, unless you have a better working suggestion, in which case, please create an issue and PR right away!!!
- Use csrf tokens for form login
- Let me know!
- As this repo is still in its baby stages, any suggestions/improvements/bugs can be in the form of Pull Requests, or creating an issue.
- Coding guidelines:
Credits to webpack-server-side-example, example-app, flux-examples, node-express-mongo-demo, hackathon-starter, web-starter-kit, awesome material-ui, alt and iso, react-starter, reap.
MIT