Using React with Golang? and does it make sense?

5,790 views
Skip to first unread message

so.q...@gmail.com

unread,
Feb 6, 2017, 12:09:10 PM2/6/17
to golang-nuts
Correct me if I'm wrong, but in serving web apps and sites.

Golang simply fills in the blanks for predefined templates.

React appears to be a bit more complicated requiring the creation of JavaScript/JSX classes representing the view. Having that rendered on the server-side and the difference applied on the client.

To combine the two, do I need to run Node.js on the server to process the React/JSX classes before piping the results to Golang for responding to the client? So Golang would be the conduit/middle-man filling in for Express here.

Does this even make sense? If I'm already running Node.js, does the use of Golang feel superfluous?




iv...@ranomics.com

unread,
Feb 6, 2017, 7:19:04 PM2/6/17
to golang-nuts
You don't have to use server-side rendering. You can have the React client make ajax requests to your Go backend.

Kevin Powick

unread,
Feb 6, 2017, 7:29:43 PM2/6/17
to golang-nuts
React is one of many client side (browser) JavaScript frameworks for building user interfaces.

JSX is not required when using React, but is recommended as it gives you the full power of JavaScript to describe your rendered HTML.

Node is not required.  If you're using Go, use it to build a server-side JSON API and, optionally, act as your web server.

Our last project was a Go based API service with the HTML/JS/CCS served-up by Caddy (a web server written in Go).  On the client side we used VueJS, one of the bazillion alternatives to React.

It's all works quite well, and I would recommend a similar approach for those looking to use a Go based solution.  i.e. Go JSON API services with your web server and JS framework of choice.

--
Kevin Powick

DC

unread,
Feb 6, 2017, 7:53:43 PM2/6/17
to golang-nuts
Yes , it makes sense to me .  You can use golang as an api server . Node helps you in SEO . Probably you can have a look at https://github.com/erikras/react-redux-universal-hot-example.

Thanks,
charan

so.q...@gmail.com

unread,
Feb 6, 2017, 11:37:15 PM2/6/17
to golang-nuts
Thanks for the comment.

You said that Node is not required. But I thought react components are actually JavaScript objects. Would my Golang API server be responding back to requests with those JavaScript objects for the React library to render on the client's browser?


ma...@influxdb.com

unread,
Feb 6, 2017, 11:52:36 PM2/6/17
to golang-nuts
A little over a year ago, I experimented with server-side React rendering in Go [1] via otto [2].

At least back then, otto wasn't fully ECMA5-compliant due to not supporting negative lookaheads in regular expressions;
I had to open a PR on React [3] to remove one negative lookahead in the render path.

I have no idea if otto can run the current version of React, and it's been so long since I've tried using otto with React that I won't be able to answer any questions about that setup.

Kevin Powick

unread,
Feb 6, 2017, 11:57:23 PM2/6/17
to golang-nuts

On Monday, 6 February 2017 23:37:15 UTC-5, so.q...@gmail.com wrote:

You said that Node is not required. But I thought react components are actually JavaScript objects.

They are.  They execute on the client side, in the web browser.

 
Would my Golang API server be responding back to requests with those JavaScript objects for the React library to render on the client's browser?


Your Go API service would be responding with data (presumably from a database), formatted as JSON.  The data would have been requested by the React objects executing in the client browser.

Example

Suppose you have a Go service that returns details about a customer for a given a customer ID.  The endpoint might be an HTTP GET request that looks like the following

http://myService/customer/123456

The details are returned in JSON format

{"Id":123456,"Name":"Bob Smith","Age":25}


On the client side, you would have used JavaScript/React to make the API Call (HTTP GET) and populate your React object with the returned data, which would then be rendered in your browser UI (page).

The advantage of your Go service simply returning JSON data is that it can be consumed by any client software capable of an HTTP request.  This make it available to any number of languages with HTTP Client libraries available.  These days, that's pretty much any language you would care to work with.  

--
Kevin Powick

Сергей Камардин

unread,
Feb 7, 2017, 8:31:16 AM2/7/17
to golang-nuts
My opinion is that server side rendering is just temporary thing for search engine optimizations.

But, if you really need it, you could use Node.js as client to your go api. Then, nginx as a front proxy, could serve static assets with and without backend data from go api. For example, if Node.js is running, then proxy index.html request there. If not just reply with empty html that will be filled by requests from browser to your go api.

so.q...@gmail.com

unread,
Feb 7, 2017, 11:56:08 AM2/7/17
to golang-nuts
Thanks for going into detail for me!
Reply all
Reply to author
Forward
0 new messages