designetwork(EN)

IT technical memo of networking

HTTP communication of Node.js is recommended to use then-request (sync-request is deprecated)

f:id:daichi703n:20170221232447p:plain

As introduced in this article, the sync-request module can perform HTTP communication by synchronous processing. This makes simple code without callback processing by asynchronous processing peculiar to javascript.

However, on the sync-request page of npm, switching to deprecated and then-request is recommended for commercial use. (Confirmed February 21, 2017)

npm announcement

On the sync-request page of npm, it is announced as follows.

www.npmjs.com

N.B. You should not be using this in a production application. In a node.js application you will find that you are completely unable to scale your server. In a client application you will find that sync-request causes the app to hang/freeze. Synchronous web requests are the number one cause of browser crashes. For production apps, you should use then-request, which is exactly the same except that it is asynchronous.

Because we are using sync-request of synchronous processing avoiding asynchronism, the difference is not accepted. However, since my use is a development environment, accept the warning and continue using it.

warning display with sync-request Ver.4

At the beginning of use, we used Ver.3.0.1. Execution with this package.json (snippets) results in the following result.

"sync-request": "~3.x.x"

$ npm install
sync-request@3.0.1 node_modules/sync-request
├── http-response-object@1.1.0
└── concat-stream@1.6.0 (inherits@2.0.3, typedarray@0.0.6, readable-stream@2.2.2)

Run sample program

main
->
Access function https://google.com (wait for completion by synchronization) and display Status Code
-> return Status Code
Display main status code

$ node ./returnReqSync.js 
Start  Return Request Sync
Status Code (function) : 200
Status Code (main)     : 200
End    Return Request Sync

Try using Version 4.

"sync-request": "~4.x.x"

$ npm install
sync-request@4.0.1 node_modules/sync-request
├── http-response-object@1.1.0
├── concat-stream@1.6.0 (inherits@2.0.3, typedarray@0.0.6, readable-stream@2.2.2)
└── get-port@2.1.0 (pinkie-promise@2.0.1)

When executed, a warning "Could not use "nc", falling back to slower node.js method for sync requests." is displayed. Although it is equivalent to the description of npm, it is unknown what "nc" points. Ignore it because it is not an error.

$ node ./returnReqSync.js 

Start  Return Request Sync
Could not use "nc", falling back to slower node.js method for sync requests.
Status Code (function) : 200
Status Code (main)     : 200
End    Return Request Sync

Try using the then-request module

Try rewriting it to the then-request module. See npm for usage.

var request = require('then-request');
var returnCode;

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

function httpGet(){
  //var response = request('GET', 'https://google.com/'); //pattern of sync-request
  request('GET', 'https://google.com/').done(function (response) {
    console.log("Status Code (function) : "+response.statusCode);
    return response.statusCode;
  });
}

As you can see, asynchronous processing has not been done at the display time of the main function.

$ node ./returnReqThen.js 
Start  Return Request Sync
Status Code (main)     : undefined
End    Return Request Sync
Status Code (function) : 200

Despite being deprecated, sync-request, which makes it easy to implement HTTP synchronization processing at present, is very useful. Since I am a development environment use, I will continue to use it. If there is no problem with asynchrony, switching to then-request that inherits the rich options of sync-request may be considered.


This Blog is English Version of my JP's.

Sorry if my English sentences are incorrect.

designetwork