On a Grunt-init template I would like to create a node.js https request. Is that possible?

Grunt-init console says: Warning: Protocol:https: not supported

Here is the code I am trying to add

var http = require('http');
var fs = require('fs');

var file = fs.createWriteStream("file.txt");
var request = http.get("https://someurl.com/file.text", function(response) {
  response.pipe(file);
});

Thanks.

有帮助吗?

解决方案

You want the https module. It acts just like the http module, with a few differences that only matter when you want to check certs. See the node https docs for details.

In your example you can just replace require('http') with require('https').

You probably don't want to worry about which protocol to use for every url you want to fetch, so you might want to look into a higher level library: the request library is very popular, although there are many others.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top