L
L
Lesha Fedoseev2015-04-15 12:05:58
API
Lesha Fedoseev, 2015-04-15 12:05:58

How to stream JSON API requests through Node.js + Express + React app?

There is an isomorphic React application that runs on a Node.js + Express server. Data is fetched/given via JSON API on Rails.
I want to hear opinions on how best to organize the flow of requests to the API.
Option #1
5873bce3032b42abbd62b413e5c6045d.png
The first request from the client goes to the Node.js server (1-2), which makes an http request from the server to the Rails API (3). Receives data from it (4). React renders the html and sends it to the client (5-6). Further, the client already makes ajax requests directly to the API (7-8).
In this option, the API server will have to accept CORS requests.
Option #2 A
65e748a51318412b9babac4215787b55.png
layer is added to Node.js/Express that accepts all requests to the local /api address(3) and proxies them to the API (4). Accordingly, all requests (including ajax from the client) are accepted by Express, which makes a server request to the API. Thus, we kill CORS, but we lengthen the chain.
From a security standpoint, option #2 is preferable. But how is it from other points of view compared to option #1 ?
UPDATE
Here it is described as: @medium
Here is pet-production according to the described scheme: alexfedoseev.com

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya Shatokhin, 2015-04-15
@alexfedoseev

The third option: nginx is installed as a frontend on the server, and it proxies requests to Node (for rendering) and to Rails (for ajax to "/api"). Thus, CORS requests are not needed, for the client it will look like a single server.

1
123 123, 2015-04-15
@roman01la

I would look at it this way: the Node.js server deals exclusively with the React application, the Rails server deals with the API. Putting responsibility for the API on the Node.js server adds complexity to the architecture and a lot of load on the application's rendering server, IMHO, of course.
That is, the first option is preferable. Moreover, when the Rails server is the only source of the API, it is easier to control possible future clients (more applications, let's say).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question