Node.js:node.js中的http.get方法是否有同步版本?

node.js中有没有同步版本的http.get方法?

就像是:

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

有时候这会非常有用。

不,没有。 我真的没有看到用例。

如果你扩展你的用例或者你试图解决的问题,我会试着回答这个问题。

有一个很容易使用的同步请求库。 它在内部同步生成一个subprocess,并使用then-request ,所以选项与库相似。

正如其他人所说,我会警告不要在你的运行时逻辑中使用它。 但是,加载configuration可能非常方便。

如果您正在加载configuration,另一个策略可以使用单独的脚本来启动您的过程。 例:

 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);