Node.js v4.2.4 手册 & 文档
目录
HTTPS#
稳定度: 3 - 稳定
HTTPS 是建立在 TLS/SSL 之上的 HTTP 协议。在 Node 中被实现为单独的模块。
类: https.Server#
该类是 tls.Server 的子类,并且发生和 http.Server 一样的事件。更多信息详见 http.Server。
server.setTimeout(msecs, callback)#
server.timeout#
https.createServer(options, [requestListener])#
返回一个新的 HTTPS Web 服务器对象。其中 options 类似于 tls.createServer();requestListener 是一个会被自动添加到 request 事件的函数。
实例:
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
或者
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
server.listen(port, [host], [backlog], [callback])#
server.listen(path, [callback])#
server.listen(handle, [callback])#
详见 http.listen()。
server.close([callback])#
详见 http.close()。
https.request(options, callback)#
向一个安全 Web 服务器发送请求。
options 可以是一个对象或字符串。如果 options 是字符串,它会自动被 url.parse() 解析。
所有来自 http.request() 的选项都是经过验证的。
实例:
req.on('error', function(e) {
console.error(e);
});
options 参数有如下选项
host:发送请求的服务器的域名或 IP 地址,缺省为'localhost'。hostname:为了支持url.parse(),hostname优先于host。port:远程服务器的端口,缺省为 443。method:指定 HTTP 请求方法的字符串,缺省为 `'GET'。path:请求路径,缺省为'/'。如有查询字串则应包含,比如'/index.html?page=12'。headers:包含请求头的对象。auth:基本认证,如'user:password'来计算 Authorization 头。agent:控制 Agent 行为。当使用 Agent 时请求会缺省为Connection: keep-alive。可选值有:undefined(缺省):为该主机和端口使用 globalAgent。Agent对象:明确使用传入的Agent。false:不使用 Agent 连接池,缺省请求Connection: close。
下列来自 tls.connect() 的选项也能够被指定,但一个 globalAgent 会忽略它们。
pfx:证书,SSL 所用的私钥或 CA 证书。缺省为null。key:SSL 所用私钥。缺省为null。passphrase:私钥或 pfx 的口令字符串,缺省为null。cert:所用公有 x509 证书,缺省为null。ca:用于检查远程主机的证书颁发机构或包含一系列证书颁发机构的数组。ciphers:描述要使用或排除的密码的字符串,格式请参阅 http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT。rejectUnauthorized:如为true则服务器证书会使用所给 CA 列表验证。如果验证失败则会触发'error'时间。验证过程发生于连接层,在 HTTP 请求发送之前。缺省为true。secureProtocol:所用 SSL 方法,比如SSLv3_method强制使用 SSL version 3。可取值取决于您安装的 OpenSSL 并被定义在 SSL_METHODS 常量。
要指定这些选项,使用一个自定义 Agent。
实例:
var req = https.request(options, function(res) {
...
}
或不使用 Agent。
实例:
var req = https.request(options, function(res) {
...
}
https.get(options, callback)#
类似 http.get() 但为 HTTPS。
options 可以是一个对象或字符串。如果 options 是字符串,它会自动被 url.parse() 解析。
实例:
}).on('error', function(e) {
console.error(e);
});
类: https.Agent#
类似于 http.Agent 的 HTTPS Agent 对象。详见 https.request()。
https.globalAgent#
所有 HTTPS 客户端请求的全局 https.Agent 实例。
