知识共享许可协议
本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可。

Node.js v0.10.18 手册 & 文档


HTTPS#

稳定度: 3 - 稳定

HTTPS is the HTTP protocol over TLS/SSL. In Node this is implemented as a separate module.

HTTPS 是建立在 TLS/SSL 之上的 HTTP 协议。在 Node 中被实现为单独的模块。

类: https.Server#

This class is a subclass of tls.Server and emits events same as http.Server. See http.Server for more information.

该类是 tls.Server 的子类,并且发生和 http.Server 一样的事件。更多信息详见 http.Server

server.setTimeout(msecs, callback)#

See http.Server#setTimeout().

详见 http.Server#setTimeout()

server.timeout#

See http.Server#timeout.

详见 http.Server#timeout

https.createServer(options, [requestListener])#

Returns a new HTTPS web server object. The options is similar to tls.createServer(). The requestListener is a function which is automatically added to the 'request' event.

返回一个新的 HTTPS Web 服务器对象。其中 options 类似于 tls.createServer()requestListener 是一个会被自动添加到 request 事件的函数。

Example:

示例:

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8000);

Or

或者

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])#

server.listen(port, [host], [backlog], [callback])#

server.listen(path, [callback])#

server.listen(handle, [callback])#

See http.listen() for details.

详见 http.listen()

server.close([callback])#

See http.close() for details.

详见 http.close()

https.request(options, callback)#

Makes a request to a secure web server.

向一个安全 Web 服务器发送请求。

options can be an object or a string. If options is a string, it is automatically parsed with url.parse().

options 可以是一个对象或字符串。如果 options 是字符串,它会自动被 url.parse() 解析。

All options from http.request() are valid.

所有来自 http.request() 的选项都是经过验证的。

Example:

示例:

req.on('error', function(e) {
  console.error(e);
});

The options argument has the following options

options 参数有如下选项

  • host: A domain name or IP address of the server to issue the request to. Defaults to 'localhost'.
  • hostname: To support url.parse() hostname is preferred over host
  • port: Port of remote server. Defaults to 443.
  • method: A string specifying the HTTP request method. Defaults to 'GET'.
  • path: Request path. Defaults to '/'. Should include query string if any. E.G. '/index.html?page=12'
  • headers: An object containing request headers.
  • auth: Basic authentication i.e. 'user:password' to compute an Authorization header.
  • agent: Controls Agent behavior. When an Agent is used request will default to Connection: keep-alive. Possible values:

    • undefined (default): use globalAgent for this host and port.
    • Agent object: explicitly use the passed in Agent.
    • false: opts out of connection pooling with an Agent, defaults request to Connection: close.
  • 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

The following options from tls.connect() can also be specified. However, a globalAgent silently ignores these.

下列来自 tls.connect() 的选项也能够被指定,但一个 globalAgent 会忽略它们。

  • pfx: Certificate, Private key and CA certificates to use for SSL. Default null.
  • key: Private key to use for SSL. Default null.
  • passphrase: A string of passphrase for the private key or pfx. Default null.
  • cert: Public x509 certificate to use. Default null.
  • ca: An authority certificate or array of authority certificates to check the remote host against.
  • ciphers: A string describing the ciphers to use or exclude. Consult http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT for details on the format.
  • rejectUnauthorized: If true, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if verification fails. Verification happens at the connection level, before the HTTP request is sent. Default true.
  • secureProtocol: The SSL method to use, e.g. SSLv3_method to force SSL version 3. The possible values depend on your installation of OpenSSL and are defined in the constant SSL_METHODS.

  • 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 常量。

In order to specify these options, use a custom Agent.

要指定这些选项,使用一个自定义 Agent

Example:

示例:

var req = https.request(options, function(res) {
  ...
}

Or does not use an Agent.

或不使用 Agent

Example:

示例:

var req = https.request(options, function(res) {
  ...
}

https.get(options, callback)#

Like http.get() but for HTTPS.

类似 http.get() 但为 HTTPS。

options can be an object or a string. If options is a string, it is automatically parsed with url.parse().

options 可以是一个对象或字符串。如果 options 是字符串,它会自动被 url.parse() 解析。

Example:

示例:

}).on('error', function(e) {
  console.error(e);
});

类: https.Agent#

An Agent object for HTTPS similar to http.Agent. See https.request() for more information.

类似于 http.Agent 的 HTTPS Agent 对象。详见 https.request()

https.globalAgent#

Global instance of https.Agent for all HTTPS client requests.

所有 HTTPS 客户端请求的全局 https.Agent 实例。