Answer the question
In order to leave comments, you need to log in
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
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
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
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.
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 questionAsk a Question
731 491 924 answers to any question