当前位置: 首页 > 文档资料 > Node.js API 文档 >

https

优质
小牛编辑
135浏览
2023-12-01

稳定性: 2 - 稳定的
HTTPS 是 HTTP 基于 TLS/SSL 的版本。在 Node.js 中,它被实现为一个独立的模块。

https.Agent 类

新增于: v0.4.5
HTTPS 的一个类似于 http.Agent 的代理对象。查看 https.request() 获取更多信息。

https.Server 类

新增于: v0.3.4
这个类是 tls.Server 的子类,跟 http.Server 一样触发事件。查看 http.Server 获取更多信息。

server.setTimeout([msecs][, callback])

新增于: v0.11.2

  • msecs <number> Defaults to 120000 (2 minutes).
  • callback <Function>

查看 http.Server#setTimeout()

server.timeout

新增于: v0.11.2

  • <number> Defaults to 120000 (2 minutes).

查看 http.Server#timeout

server.keepAliveTimeout

新增于: v8.0.0

  • <number> Defaults to 5000 (5 seconds).

See http.Server#keepAliveTimeout.

https.createServer(options[, requestListener])

新增于: v0.3.4

  • options <Object> Accepts options from tls.createServer() and tls.createSecureContext().
  • requestListener <Function> A listener to be added to the request event.

例子:

// curl -k https://localhost:8000/
const https = require('https');
const fs = require('fs');

const options = {
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};

https.createServer(options, (req, res) => {
  res.writeHead(200);
  res.end('hello world\n');
}).listen(8000);

或者

const https = require('https');
const fs = require('fs');

const options = {
  pfx: fs.readFileSync('test/fixtures/test_cert.pfx'),
  passphrase: 'sample'
};

https.createServer(options, (req, res) => {
  res.writeHead(200);
  res.end('hello world\n');
}).listen(8000);

server.close([callback])

新增于: v0.1.90

  • callback <Function>

查看 http.close() 获取细节。

server.listen(handle[, callback])

  • handle <Object>
  • callback <Function>

server.listen(path[, callback])

  • path <string>
  • callback <Function>

server.listen([port][, host][, backlog][, callback])

  • port <number>
  • hostname <string>
  • backlog <number>
  • callback <Function>

查看 http.listen() 获取细节。

https.get(options[, callback])

版本历史

版本变更
v7.5.0The options parameter can be a WHATWG URL object.
v0.3.6新增于: v0.3.6
  • options <Object> | <string> | <URL> Accepts the same options as https.request(), with the method always set to GET.
  • callback <Function>

类似 http.get(),但是用于 HTTPS。

参数 options 可以是一个对象、或字符串、或 URL 对象。 如果参数 options 是一个字符串, 它自动被 url.parse() 所解析。 If it is a URL object, it will be automatically converted to an ordinary options object.

例子:

const https = require('https');

https.get('https://encrypted.google.com/', (res) => {
  console.log('状态码:', res.statusCode);
  console.log('请求头:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });

}).on('error', (e) => {
  console.error(e);
});

https.globalAgent

新增于: v0.5.9
https.Agent 的全局实例,用于所有 HTTPS 客户端请求。

https.request(options[, callback])

版本历史

版本变更
v7.5.0The options parameter can be a WHATWG URL object.
v0.3.6新增于: v0.3.6
  • options <Object> | <string> | <URL> Accepts all options from http.request(), with some differences in default values:
    • protocol Defaults to https:
    • port Defaults to 443.
    • agent Defaults to https.globalAgent.
  • callback <Function>

向一个安全的服务器发起一个请求。

The following additional options from tls.connect() are also accepted when using a custom Agent: pfx, key, passphrase, cert, ca, ciphers, rejectUnauthorized, secureProtocol, servername

参数 options 可以是一个对象、或字符串、或 URL 对象。 如果参数 options 是一个字符串, 它自动被 url.parse() 所解析。 If it is a URL object, it will be automatically converted to an ordinary options object.

例子:

const https = require('https');

const options = {
  hostname: 'encrypted.google.com',
  port: 443,
  path: '/',
  method: 'GET'
};

const req = https.request(options, (res) => {
  console.log('状态码:', res.statusCode);
  console.log('请求头:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });
});

req.on('error', (e) => {
  console.error(e);
});
req.end();

Example using options from tls.connect():

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

const req = https.request(options, (res) => {
  // ...
});

也可以不对连接池使用 Agent

例子:

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

const req = https.request(options, (res) => {
  // ...
});

Example using a URL as options:

const { URL } = require('url');

const options = new URL('https://abc:xyz@example.com');

const req = https.request(options, (res) => {
  // ...
});