Create HTTPS client in NodeJS
Solution 1
It turns out I was using an old version of the Node documentation, which didn't include a reference to the https
module. Referring to the current docs, I found http://nodejs.org/docs/latest/api/https.html#https_https_get_options_callback, which provides an example:
https.get({ host: 'encrypted.google.com', path: '/' }, function (res) { … });
Solution 2
If you are using node.js as a client you should be able to simply substitute http for https.
That is according to the following website https://github.com/danwrong/Restler/
"Transparently handle SSL (just specify https in the URL)"
Ryan Tenney
Computer scientist, debonaire bachelor, beer connoisseur, and secular humanist. Fluent in Java, JavaScript, PHP, SQL Conversational in C, C++, C#, Scala, Go
Updated on July 22, 2022Comments
-
Ryan Tenney almost 2 years
I've been having a heck of a time figuring out how to use Node.js (v0.3.8) to securely connect to an HTTP server. I have the following code:
var http = require("http"); var client = http.createClient(443, host, /* secure= */ true); var request = client.request("GET", relativeUrl, { host: host });
When I run it, I get:
node.js:116 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: Parse Error at Client.onData [as ondata] (http.js:1287:27) at Client._onReadable (net.js:648:27) at IOWatcher.onReadable [as callback] (net.js:156:10)
I've been Googling for answers for the past half hour, and have read the documentation at http://nodejs.org/ . What am I missing?
-
Ryan Tenney over 13 yearsI'll look into using Restler, I hadn't encountered it previously. On examining the source, I did notice that it uses the
https
module, which helped me find the answer to my question. Thank you!