鍍金池/ 教程/ HTML/ HTTPS
模塊
斷言測(cè)試
Buffer
Smalloc
TTY
概述
REPL
HTTP
DNS
路徑
集群
TLS/SSL
系統(tǒng)
加密
調(diào)試器
進(jìn)程
Punycode
虛擬機(jī)
HTTPS
網(wǎng)絡(luò)
Query String
C/C++ 插件
實(shí)用工具
文件系統(tǒng)
Zlib
子進(jìn)程
UDP/Datagram Sockets
定時(shí)器
逐行讀取
字符串解碼器
全局對(duì)象
事件
URL
控制臺(tái)

HTTPS

穩(wěn)定性: 3 - 穩(wěn)定

HTTPS 是基于 TLS/SSL 的 HTTP 協(xié)議。在 Node 里作為單獨(dú)的模塊來(lái)實(shí)現(xiàn)。

類: https.Server

這是 tls.Server 的子類,并且和 http.Server 一樣觸發(fā)事件。更多信息參見 http.Server 。

server.setTimeout(msecs, callback)

詳情參見 http.Server#setTimeout().

server.timeout

詳情參見 http.Server#timeout.

https.createServer(options[, requestListener])

返回一個(gè)新的 HTTPS 服務(wù)器對(duì)象。其中 options 類似于 [tls.createServer()][tls.md#tls_tls_createserver_options_secureconnectionlistener]。 requestListener 函數(shù)自動(dòng)加到 '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)

發(fā)送請(qǐng)求到安全 web 服務(wù)器。

options 可以是一個(gè)對(duì)象或字符串。如果 options 是字符串。會(huì)被 url.parse() 解析。

所有來(lái)自 http.request() 選項(xiàng)都是經(jīng)過驗(yàn)證的。

例如:

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);
});

option 參數(shù)有以下的值:

  • host: 請(qǐng)求的服務(wù)器域名或 IP 地址,默認(rèn):'localhost'
  • hostname: 用于支持 url.parse()hostname 優(yōu)于 host
  • port: 遠(yuǎn)程服務(wù)器端口。 默認(rèn): 443.
  • method: 指定 HTTP 請(qǐng)求方法。 默認(rèn): 'GET'.
  • path: 請(qǐng)求路徑。 默認(rèn): '/'。如果有查詢字符串,則需要包含。比如 '/index.html?page=12'
  • headers: 包含請(qǐng)求頭的對(duì)象
  • auth: 用于計(jì)算認(rèn)證頭的基本認(rèn)證,即user:password
  • agent: 控制Agent的行為。當(dāng)使用了一個(gè)Agent的時(shí)候,請(qǐng)求將默認(rèn)為Connection: keep-alive??赡艿闹禐椋?
    • undefined (default): 在這個(gè)主機(jī)和端口上使用 [global Agent][]
    • Agent object: 在Agent中顯式使用 passed.
    • false: 選擇性停用連接池,默認(rèn)請(qǐng)求為: Connection: close

tls.connect() 的參數(shù)也能指定。但是,globalAgent 會(huì)忽略他們。

  • pfx: SSL 使用的證書,私鑰,和證書Certificate, 默認(rèn) null.
  • key: SSL 使用的私鑰. 默認(rèn) null.
  • passphrase: 私鑰或 pfx 的口令字符串. 默認(rèn) null.
  • cert: 所用公有 x509 證書. 默認(rèn) null.
  • ca: 用于檢查遠(yuǎn)程主機(jī)的證書頒發(fā)機(jī)構(gòu)或包含一系列證書頒發(fā)機(jī)構(gòu)的數(shù)組。
  • ciphers: 描述要使用或排除的密碼的字符串,格式請(qǐng)參閱 http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT
  • rejectUnauthorized: 如為 true 則服務(wù)器證書會(huì)使用所給 CA 列表驗(yàn)證。如果驗(yàn)證失敗則會(huì)觸發(fā) error 事件。驗(yàn)證過程發(fā)生于連接層,在 HTTP 請(qǐng)求發(fā)送之前。缺省為 true.
  • secureProtocol: 所用的 SSL 方法,比如 TLSv1_method 強(qiáng)制使用 TLS version 1??扇≈等Q于您安裝的 OpenSSL, 和定義于 SSL_METHODS 的常量。

要指定這些選項(xiàng),使用一個(gè)自定義 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 可以是字符串對(duì)象. 如果 options 是字符串, 會(huì)自動(dòng)使用 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);
});

類: https.Agent

HTTPS 的 Agent 對(duì)象,和 http.Agent 類似。 詳情參見 https.request()

https.globalAgent

所有 HTTPS 客戶端請(qǐng)求的 https.Agent 全局實(shí)例。

下一篇:TLS/SSL