在NodeJS中创建HTTPS客户端 [英] Create HTTPS client in NodeJS
问题描述
我一直想知道如何使用Node.js(v0.3.8)安全地连接到HTTP服务器。我有以下代码:
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)
过去半小时我一直在谷歌搜索答案,并阅读了 http://nodejs.org/ 。我缺少什么?
I've been Googling for answers for the past half hour, and have read the documentation at http://nodejs.org/ . What am I missing?
推荐答案
事实证明我使用的是旧版本的Node文档,其中不包含引用 https
模块。参考当前的文档,我找到了 http://nodejs.org/docs/latest /api/https.html#https_https_get_options_callback ,提供了一个示例:
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) { … });
这篇关于在NodeJS中创建HTTPS客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!