Javascript 在 NodeJS 中创建 HTTPS 客户端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4940646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create HTTPS client in NodeJS
提问by Ryan Tenney
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:
我一直在研究如何使用 Node.js (v0.3.8) 安全地连接到 HTTP 服务器。我有以下代码:
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?
过去半个小时我一直在谷歌上搜索答案,并阅读了http://nodejs.org/ 上的文档。我错过了什么?
回答by Ryan Tenney
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:
事实证明,我使用的是旧版本的 Node 文档,其中不包含对https
模块的引用。参考当前的文档,我找到了http://nodejs.org/docs/latest/api/https.html#https_https_get_options_callback,它提供了一个示例:
https.get({ host: 'encrypted.google.com', path: '/' }, function (res) { … });
回答by Nick
If you are using node.js as a client you should be able to simply substitute http for https.
如果您使用 node.js 作为客户端,您应该能够简单地将 http 替换为 https。
That is according to the following website https://github.com/danwrong/Restler/
这是根据以下网站 https://github.com/danwrong/Restler/
"Transparently handle SSL (just specify https in the URL)"