double action in node.js

deniska
Participant V

Hi,

Here's an interesting one:

User's browser will receive 302 and will be redirected to API proxy in Apigee, with some query params. Apigee proxy, for example, /landing, will generate HTML code with 'Please wait" and spinner effect (gif or js).

My goal is to 'hold' this HTML page and in the backend do something, for example, extract the query param and send them to the backend. Once backend responded with 200, I will refresh the page "Please wait" with "success" for example.

I plan to do that with node.js (for HTML). Problem is that once HTML rendered, session is closed (html will return to user browser with HTTP 200). I need to keep the page rendered but some how push to that page refresh back. Are there any suggestions to achieve this specific goal?

0 3 172
3 REPLIES 3

Your question is not directly related to Apigee Edge. Rather, it relates to HTTP clients and servers in general.

If your client POSTs a request to a server and the server responds with a "please wait" page with a spinner gif, THAT is the response. The HTTP protocol works on the request/response model. When the client receives the response, that is the response. If your application - the web browser - ties its UI to the request/response model, then... the browser will only display the "please wait" response.

The way to construct an experience like what you described, is to separate the UI from the request/response communication protocol. This means you must use JavaScript in the web app to send requests "Asynchronously" with respect to page update.

A web page can load, then... send an API request in the background, and at the same time display a "please wait" spinner. At some point later, the response for that API request arrives, and the JS running in the page can update the page to display data. This was originally termed "AJAX" for Asynchronous JavaScript and XML, when API response payloads were commonly XML. but AJAX or "asynch JavaScript" obviously works with any response payload; XML is not required and these days most people use JSON because it is so easy to parse in client-side JavaScript.

So checkout AJAX. There are numerous "frameworks" for client-side web apps to consider. React, jQuery, Angular... they are all different app metaphors, but each one has a mechanism for performing asynch javascript and updating a page with the data returned in the response.

I suggest you follow a tutorial or two for that, and then later try to figure out how to include Apigee Edge into the mix to solve whatever problem you need to solve.

Sure Dino, we are fully aware how the API\HTTP model works, this is exactly the problem I'm facing - if response returned, it is lost, stateless.

In our case app is redirecting user (302) to location where I will build the page (similar to OAUTH2.0 redirect for authorization), but, in OAUTH2.0 user interacts and clicks something, here I want him to just wait and recieve 'Loading' and after 1-3 seconds "completed".

I'm thinking on bulding proxy, where HTML is rendered in Node.js app, and in the end Node.js will call other API and will not render HTML until the end, like you described. The question is can I do this inside node.js app inside the Apigee, because we will likely will not build web application only for this screen functionality.

Cheers;

OK, I don't understand the problem.

I will try again to express my perspective. If your wish is to be able to displaying a spinner in a client app while a request is pending, then. . . . I don't think you need a nodejs implementation for that. The CLIENT is the thing that needs to be aware of the behavior of the request/response, and the client is the thing that must have the intelligence to display the spinner while waiting for a response.

In the case of a browser page, the "client" can be implemented as JavaScript that makes XHR requests (aka AJAX). If the client is not running in a web app ("we will likely will not build web application only for this screen functionality..."), then that other client needs to have a parallel function, some other way to communicate to the user "a response is pending".

Building a nodejs target in Apigee Edge will not affect the client.

Sorry I know this answer won't be satisfactory. I'm sure I'm not understanding the problem completely.