JSRUN 用代码说话

HTTPS

编辑教程

HTTPS

稳定性: 3 - 稳定

HTTPS是什么?HTTPS是基于TLS/SSL的HTTP协议,在Node.js里它可以作为单独的模块来实现。在本文中,你将了解HTTPS的使用方法。

类: https.Server

https.Server是tls.Server的子类,并且和http.Server一样触发事件。

server.setTimeout(msecs, callback)

server.timeout

https.createServer(options[, requestListener])

返回一个新的HTTPS服务器对象。其中options类似于 [tls.createServer()][tls.md#tls_tls_createserver_options_secureconnectionlistener]。 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])

server.close([callback])

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

option参数具有以下的值:

  • host : 请求的服务器域名或IP地址,默认: 'localhost'
  • hostname : 用于支持 url.parse() 。 hostname 优于 host
  • port : 远程服务器端口。默认: 443。
  • method : 指定HTTP请求方法。默认: 'GET' 。
  • path : 请求路径。默认: '/' 。如果有查询字符串,则需要包含。比如 '/index.html?page=12'
  • headers : 包含请求头的对象
  • auth : 用于计算认证头的基本认证,即 user:password
  • agent : 控制Agent的行为。当使用了一个Agent的时候,请求将默认为 Connection: keep-alive 。可能的值为:

    • undefined (default): 在这个主机和端口上使用[global Agent][]
    • Agent object: 在 Agent 中显式使用passed.
    • false : 选择性停用连接池,默认请求为: Connection: close

    tls.connect() 的参数也能指定。但是, globalAgent 会忽略他们。

  • pfx : SSL使用的证书,私钥,和证书Certificate,默认为 null .

  • key : SSL使用的私钥. 默认为 null .
  • passphrase : 私钥或pfx的口令字符串. 默认为 null .
  • cert : 所用公有x509证书. 默认为 null .
  • ca : 用于检查远程主机的证书颁发机构或包含一系列证书颁发机构的数组。
  • ciphers : 描述要使用或排除的密码的字符串
  • rejectUnauthorized : 如为 true 则服务器证书会使用所给CA列表验证。如果验证失败则会触发 error 事件。验证过程发生于连接层,在 HTTP 请求发送之前。默认为 true .
  • secureProtocol : 所用的SSL方法,比如 TLSv1_method 强制使用TLS version 1。可取值取决于您安装的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);
});

类: https.Agent

HTTPS的Agent对象,和 http.Agent 类似。

https.globalAgent

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

JSRUN闪电教程系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的闪电教程,让知识的积累变得统一完整、自成体系。 大家可以一起参与进共编,让零散的知识点帮助更多的人。
X
支付宝
9.99
无法付款,请点击这里
金额: 0
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间
如有疑问请联系QQ:565830900
正在生成二维码, 此过程可能需要15秒钟