Introduction

The purpose of this article is to demonstrate how you can serve your React.js Single Page Application (SPA) with a Go server and combine the entire application into a single binary file, able to run in any operating system.

Prerequisites

For this tutorial you will need:

  • Go environment (instructions)
  • Node.js environment (instructions)
  • Go package go.rice ($ go get ``[github.com/GeertJohan/go.rice](https://github.com/GeertJohan/go.rice))
  • Create-react-app tool ($ npm i create-react-app)
  • React library react-router-dom ($ npm i react-router-dom)
  • A new directory, e.x react-go-binary , inside your GOPATH where all of our code will live

Create our React.js SPA

As a first step, we are going to generate our React.js frontend client using the wonderful CLI tool _create-react-app _developed by the React Engineer Team at Facebook.

All we have to do is to run $ npx create-react-app ./client inside our project directory (e.x. react-go-binary) and a new directory client will be created, including pretty standard source files.

The client will include a simple navigation bar and two (2) pages, a home and a ping page with a button to make a http request on our REST API.

We will put our logic on the src/App.js file regarding router, pages and render our application from the src/index.js file.

Define the App functional component which includes the router, the home component, the ping component and the navbar component on the src/App.js file.

#reactjs #golang #go #cross-platform #react

Combine a React.js and Go app into a single binary file
9.05 GEEK