# HTTPS
~~~
穩(wěn)定度: 3 - 穩(wěn)定
~~~
HTTPS 是建立在 TLS/SSL 之上的 HTTP 協(xié)議。在 Node 中被實(shí)現(xiàn)為單獨(dú)的模塊。
### 類: https.Server
該類是 `tls.Server` 的子類,并且發(fā)生和 `http.Server` 一樣的事件。更多信息詳見 `http.Server`。
### server.setTimeout(msecs, callback)
詳見 [http.Server#setTimeout()](#)。
### server.timeout
詳見 [http.Server#timeout](#)。
### https.createServer(options, [requestListener])
返回一個(gè)新的 HTTPS Web 服務(wù)器對象。其中 `options` 類似于 [tls.createServer()](#);`requestListener` 是一個(gè)會被自動添加到 `request` 事件的函數(shù)。
實(shí)例:
~~~
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)
向一個(gè)安全 Web 服務(wù)器發(fā)送請求。
`options` 可以是一個(gè)對象或字符串。如果 `options` 是字符串,它會自動被 [url.parse()](#) 解析。
所有來自 [http.request()](#) 的選項(xiàng)都是經(jīng)過驗(yàn)證的。
實(shí)例:
~~~
req.on('error', function(e) {
console.error(e);
});
~~~
options 參數(shù)有如下選項(xiàng)
- `host`:發(fā)送請求的服務(wù)器的域名或 IP 地址,缺省為 `'localhost'`。
- `hostname`:為了支持 `url.parse()`,`hostname` 優(yōu)先于 `host`。
- `port`:遠(yuǎn)程服務(wù)器的端口,缺省為 443。
- `method`:指定 HTTP 請求方法的字符串,缺省為 `'GET'。
- `path`:請求路徑,缺省為 `'/'`。如有查詢字串則應(yīng)包含,比如 `'/index.html?page=12'`。
- `headers`:包含請求頭的對象。
- `auth`:基本認(rèn)證,如 `'user:password'` 來計(jì)算 Authorization 頭。
- `agent`:控制 [Agent](#) 行為。當(dāng)使用 Agent 時(shí)請求會缺省為 `Connection: keep-alive`??蛇x值有:
- `undefined`(缺?。簽樵撝鳈C(jī)和端口使用 [globalAgent](#)。
- `Agent` 對象:明確使用傳入的 `Agent`。
- `false`:不使用 Agent 連接池,缺省請求 `Connection: close`。
下列來自 [tls.connect()](#) 的選項(xiàng)也能夠被指定,但一個(gè) [globalAgent](#) 會忽略它們。
- `pfx`:證書,SSL 所用的私鑰或 CA 證書。缺省為 `null`。
- `key`:SSL 所用私鑰。缺省為 `null`。
- `passphrase`:私鑰或 pfx 的口令字符串,缺省為 `null`。
- `cert`:所用公有 x509 證書,缺省為 `null`。
- `ca`:用于檢查遠(yuǎn)程主機(jī)的證書頒發(fā)機(jī)構(gòu)或包含一系列證書頒發(fā)機(jī)構(gòu)的數(shù)組。
- `ciphers`:描述要使用或排除的密碼的字符串,格式請參閱 [http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT](http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT)。
- `rejectUnauthorized`:如為 `true` 則服務(wù)器證書會使用所給 CA 列表驗(yàn)證。如果驗(yàn)證失敗則會觸發(fā) `'error'` 時(shí)間。驗(yàn)證過程發(fā)生于連接層,在 HTTP 請求發(fā)送*之前*。缺省為 `true`。
- `secureProtocol`:所用 SSL 方法,比如 `SSLv3_method` 強(qiáng)制使用 SSL version 3。可取值取決于您安裝的 OpenSSL 并被定義在 [SSL_METHODS](http://www.openssl.org/docs/ssl/ssl.html#DEALING_WITH_PROTOCOL_METHODS) 常量。
要指定這些選項(xiàng),使用一個(gè)自定義 `Agent`。
實(shí)例:
~~~
var req = https.request(options, function(res) {
...
}
~~~
或不使用 `Agent`。
實(shí)例:
~~~
var req = https.request(options, function(res) {
...
}
~~~
### https.get(options, callback)
類似 `http.get()` 但為 HTTPS。
`options` 可以是一個(gè)對象或字符串。如果 `options` 是字符串,它會自動被 [url.parse()](#) 解析。
實(shí)例:
~~~
}).on('error', function(e) {
console.error(e);
});
~~~
### 類: https.Agent
類似于 [http.Agent](#) 的 HTTPS Agent 對象。詳見 [https.request()](#)。
### https.globalAgent
所有 HTTPS 客戶端請求的全局 [https.Agent](#) 實(shí)例。
- 關(guān)于本文檔
- 概述
- 斷言 (assert)
- Buffer
- Addons插件
- 子進(jìn)程
- 集群
- 控制臺
- 加密(Crypto)
- 調(diào)試器
- DNS
- 域
- 事件 (Events)
- File System
- 全局對象
- HTTP
- HTTPS
- Modules
- net
- 操作系統(tǒng)
- 路徑 (Path)
- process
- punycode
- Query String
- Readline
- REPL
- Smalloc
- 流
- 字符串解碼器
- 定時(shí)器
- TLS (SSL)
- TTY
- UDP / 數(shù)據(jù)報(bào)套接字
- URL
- utils
- 執(zhí)行 JavaScript
- Zlib
- 進(jìn)度
- 感謝
