### 穩定度: 2 - 穩定
HTTPS是建立在TLS/SSL之上的HTTP協議。在`io.js`中,它被作為單獨模塊實現。
#### Class: 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`事件的監聽器。
例子:
~~~
// curl -k https://localhost:8000/
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
~~~
或
~~~
var https = require('https');
var fs = require('fs');
var options = {
pfx: fs.readFileSync('server.pfx')
};
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()`選項都是可用的。
例子:
~~~
var https = require('https');
var options = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
method: 'GET'
};
var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
~~~
`options`參數有以下選項:
- host: 一個將要向其發送請求的服務器域名或IP地址。默認為`localhost`。
- hostname: `host`的別名。為了支持`url.parse()`的話,`hostname`比`host`更好些。
- family: 解析`host`和`hostname`時的IP地址協議族。合法值是`4`和`6`。當沒有指定時,將都被使用。
- port: 遠程服務器端口。默認為`80`。
- localAddress: 用于綁定網絡連接的本地端口。
- socketPath: Unix域`socket`(使用`host:port`或`socketPath`)。
- method: 指定HTTP請求方法的字符串。默認為`GET`。
- path: 請求路徑。默認為`/`。如果有查詢字符串,則需要包含。例如'/index.html?page=12'。請求路徑包含非法字符時拋出異常。目前,只否決空格,不過在未來可能改變。
- headers: 一個包含請求頭的對象。
- auth: 用于計算認證頭的基本認證,即`'user:password'`。
- **agent**: 控制`agent`行為。當使用一個代理時,請求將默認為`Connection: keep-alive`。可能值有:
- undefined (默認): 在這個主機和端口上使用全局`agent。
- Agent object: 在`agent`中顯示使用passed。
- 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 v3。可用的值取決你的OpenSSL安裝和`SSL_METHODS`常量。
要指定這些選項,使用一個自定義的`Agent`。
例子:
~~~
var options = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
method: 'GET',
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
options.agent = new https.Agent(options);
var req = https.request(options, function(res) {
...
}
~~~
或不使用`Agent`。
例子:
~~~
var options = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
method: 'GET',
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem'),
agent: false
};
var req = https.request(options, function(res) {
...
}
~~~
#### https.get(options, callback)
類似于`http.get()`,但是使用HTTPS。
`options`可以是一個對象或一個字符串。如果`options`是一個字符串,它會自動被`url.parse()`解析。
例子:
~~~
var https = require('https');
https.get('https://encrypted.google.com/', function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
}).on('error', function(e) {
console.error(e);
});
~~~
#### Class: https.Agent
一個與`http.Agent`類似的HTTPS `Agent`對象。更多信息請參閱`https.request()`。
#### https.globalAgent
所有HTTPS客戶端請求的全局`https.Agent`實例。