Node.js: Is there a synchronous version of the http.get method in Node.js?

Is there a synchronous version of the http.get method in node.js?

Something like:

 http.getSync({ host: 'google.com', port: 80, path: '/' }, function(response){ }); console.log(response) 

Sometimes this would be very helpful.

+8
asynchronous get synchronous
source share
2 answers

No no. I honestly do not see a precedent.

If you expand your use case or the problem you are trying to solve, I will try to answer this question.

-6
source

There is a sync-request library that is pretty easy to use. Internally, it synchronously generates a child process and uses then-request , so the parameters are similar to this library.

As others have stated, I would caution against using this in your execution logic. However, this can be very convenient for loading the configuration.

If you download a configuration, another strategy may use a separate script to start your process. Example:

 var http = require("http"), cp = require("child_process"); // Starting process if (process.argv.length < 3) { return http.get("http://www.google.com/index.html", function(res) { var config = { statusCode : res.statusCode, headers : res.headers }; cp.fork(module.filename, [JSON.stringify(config)]); }); } // Config provided var config = JSON.parse(process.argv[2]); console.log(config.statusCode); 
+2
source

All Articles