designetwork(EN)

IT technical memo of networking

Return value from request module with synchronization. (Node.js)(Not Callback)

f:id:daichi703n:20161117001830j:plain

Node.js is a JavaScript that runs without a web browser and can be used on the server side. You can develop services using Web application framework such as Express. You can also automate testing by combining with test frameworks such as Selenium and mocha.

Because it can be used easily, it is convenient for a bit of API access. At that time, describe a method to return the return value with the request module for HTTP GET, POST.

You can use the callback function in a general way, but in the case of a program that can not obtain processing speed, synchronization processing is easier to handle than asynchronous.

In conclusion, the request module can not synchronize without using the general JavaScript callback function. Therefore, use the sync-request module to synchronize processing.

Using the request module

Install the request module.

$ npm install request

Simply access Google. Let the status code be the return value.

returnReqAsync.js

var request = require('request');
var returnCode;
var getUrl = 'https://google.com/';

console.log("Start  Return Request Async");
returnCode = httpGet(getUrl);
console.log("Status Code (main)     : "+returnCode);
console.log("End    Return Request Async");

function httpGet(url){
  var options = {
    url: url,
    method: 'GET',
    };
  request(options, function (error, response, body) {
    if (!error) {
      console.log("Status Code (function) : "+response.statusCode);
      return response.statusCode;
    }
    else
    {
      console.log("Error!!");
    }
  });
}

Then it becomes the following output.

$ node returnReqAsync.js

Start Return Request Async
Status Code (main) : undefined
End Return Request Async
Status Code (function) : 200

The Status Code of the main function is undefined, which indicates that there is no return value. Also, console.log in the function is output after End.

The cause of this behavior is asynchronous processing for speeding up JavaScript. The next process proceeds without waiting for the completion of httpGet processing.

Using sync-request module

Install the sync-request module.

$ npm install sync-request

Do the same operation with the sync-request module. Adjustment is made according to usage. (The basic operation is the same)

returnReqSync.js

var request = require('sync-request');
var returnCode;
var getUrl = 'https://google.com/';

console.log("Start  Return Request Sync");
returnCode = httpGet(getUrl);
console.log("Status Code (main)     : "+returnCode);
console.log("End    Return Request Sync");

function httpGet(url){
  var response = request(
    'GET',
    url
    );
    console.log("Status Code (function) : "+response.statusCode);
    return response.statusCode;
}

As a result, the synchronization process is performed as follows, the return value is returned as expected, and it is received with the main function.

$ node returnReqSync.js

Start Return Request Sync
Status Code (function) : 200
Status Code (main) : 200
End Return Request Sync

Conclusion

You can synchronize by using sync-request module when HTTP GET and POST with Node.js (javascript).

It is applicable when you wish to synchronize processing, such as acquiring information from the API (GET) and sending it to another API (POST).


This Blog is English Version of my JP's.

Sorry if my English sentences are incorrect.

designetwork