Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Last active November 2, 2017 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/c52378a63230b0b3298635a4200e1ec7 to your computer and use it in GitHub Desktop.
Save xeoncross/c52378a63230b0b3298635a4200e1ec7 to your computer and use it in GitHub Desktop.
Setting up hot-reload with react and a server for auth

React + Node Server

We want to be able to use express/hapi/feather/etc using passport for auth and only load our react app after successful login. We also want ES6, live-reloading, and hot-loading for fast development.

A common solution is to run create-react-app on one port and node server.js on another. Then you add a proxy: http://localhost:3000 to the create-react-app package.json file and it allows your react app to talk to the express server. When you are ready to deploy to production, you generate static files and only need node server.js from them on. That works as long as you don't need the express/hapi/koa/server:

  • to be the first point of entry
  • to provide inital state (you can use componentDidMount() + AJAX to load inital state)

Another solution is to simply have your node server load webpack/babel as needed for development, but default to static files for production.

// we only want hot reloading in development
if (process.env.NODE_ENV !== 'production') {
    webpackSetupGoesHere(app);
} else {
    //Production needs physical files! (built via separate process)
    app.use('/js', express.static(__dirname + '/public/dist/js'));
}

Tutorials and Guides

Example Server + Client Projects

Tools

Other Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment