Hacker News new | ask | show | jobs
Ask HN: Using React with Golang? and does it make sense?
6 points by hn-query 3420 days ago
Correct me if I'm wrong, but in serving web apps and sites.

Golang simply fills in the blanks for predefined templates on the server with the response replacing the content in its entirety on the client.

React appears to be a bit more complicated requiring the creation of JavaScript/JSX classes representing the view. Having that rendered on the server 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 (or middle-man) filling in for Express.js here.

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

2 comments

You don't need Node.js to be running as a server to use React. Most people would use it to build their react app which would be served as static file(s) and communicate with some backend APIs. The backend API could be written in Golang. It would serve data which React would render in the browser or take requests for actions such as logging in, adding a comment to the database, etc...
The data being returned in the response from the Golang API server, would that be the React view components in the form of JavaScript objects?
The back end should return data, in the form of json objects, xml, plain text, animated cat gifs, etc.
You have different options:

The first one is to use NodeJS as your front end server and Golang for your API.

The second one is to use NodeJS with React as an internal micro service to returns the HTML content back to the Golang front end server.

The last one is to use a JavaScript interpreter within Golang. Some developers are doing the same in Java. Check this project for this option: https://github.com/robertkrimen/otto

Now the question is which one is good. As always it depends on what you're looking for. I would say the NodeJS micro service renderer would be my favorite as it keeps all the complex logic in Golang.

Could you explain NodeJS + Golang API (option 1)? How should Golang handle the API if I'm using a NodeJS/Express server as well?

Golang + NodeJS microservice (option 2) is the same as the example I gave right? Where the client only communicates with a Golang server, which forwards the request to NodeJS/React for processing. The processed response is then returned by the Golang server.

Golang + JS interpreter (option 3), again the client exclusively communicates with a Golang server. But Golang types are translated to the equivalent React JS components. Then processed with NodeJS/React and returned as a response by the Golang server. Is that all right?

For the first option, you just consider your API endpoint as an RPC made in Golang and consume the content in NodeJS/Express.

For the second option, you're right.

For the third one, to make it simple, it's a V8 engine inside Golang. It's not another process or server.

Thanks for confirming!