# HTTPS
~~~
穩定度: 3 - 穩定
~~~
HTTPS 是建立在 TLS/SSL 之上的 HTTP 協議。在 Node 中被實現為單獨的模塊。
### 類: https.Server
該類是 `tls.Server` 的子類,并且發生和 `http.Server` 一樣的事件。更多信息詳見 `http.Server`。
### server.setTimeout(msecs, callback)
詳見 [http.Server#setTimeout()](#)。
### server.timeout
詳見 [http.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](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](http://www.openssl.org/docs/ssl/ssl.html#DEALING_WITH_PROTOCOL_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](#) 實例。