zacfukuda / universal-app-react-router

Super Simple Universal React App with React Router & Express.js
https://www.mokuji.me/article/universal-app-react-router
MIT License
6 stars 4 forks source link
expressjs fetch-api js nodejs react react-router tutorial universal

Simple Universal React App with React Router & Express.js

Published August 12, 2017

This repository is a sample code of the very, very simple Universal React Application, using React Router version 4 and Express.js.

The official tutorial is available at https://www.mokuji.me/article/universal-app-react-router.

Motivation

  1. Centralized Routes

    With the support of React Router Config, all routes are managed in src/routes.js, and the rendering code was optimized. This also enables us to render data components on the server.

  2. Server-side Rendering with Fetch Data

    Rather than the data are pre-defined as a variable inside Javascript file, the app fetches books data which is saved as in JSON file and renders data component on both front and server side.

Pages

The application has the following pages.

Scripts

This project was initialized with create-react-app. However, some modifications have been done in order to make it universal.

Use

Clone the repo and change the working directory:

$ git clone https://github.com/zacfukuda/universal-app-react-router
$ cd universal-app-react-router

Install the packages:

$ yarn
// Or if you use npm:
$ npm install

Client-side Single Page App

To develop as a client-side single-page app:

$ yarn start

The command will open the browser navigating to localhost:3000 and the browser will be refreshed every time you modify the files under src directory.

Universal App

Build & run the app:

$ yarn build
$ yarn server

Visit localhost:3000 and the source of the home page or the books page. You’ll see the all contents are rendered on the server.

Server-side Development

Build & watch file changes:

$ yarn watch

Run the app in the development mode:

$ yarn server-dev

Run the two command above in parallel:

$ yarn server-dev:watch

Most of the cases, server-dev:watch is sufficient.

Reference

Doc & Repo

Tutorial & Sample Code