- assert - 断言
- Buffer - 缓冲器
- child_process - 子进程
- cluster - 集群
- console - 控制台
- crypto - 加密
- dgram - 数据报
- dns - 域名服务器
- Error - 异常
- events - 事件
- fs - 文件系统
- global - 全局变量
- http - HTTP
- https - HTTPS
- module - 模块
- net - 网络
- os - 操作系统
- path - 路径
- process - 进程
- querystring - 查询字符串
- readline - 逐行读取
- repl - 交互式解释器
- stream - 流
- string_decoder - 字符串解码器
- timer - 定时器
- tls - 安全传输层
- tty - 终端
- url - 网址
- util - 实用工具
- v8 - V8引擎
- vm - 虚拟机
- zlib - 压缩
Node.js v10.8.0 文档
目录
https#
HTTPS 是 HTTP 基于 TLS/SSL 的版本。在 Node.js 中,它被实现为一个独立的模块。
https.Agent 类#
HTTPS 的一个类似于 http.Agent 的代理对象。查看 https.request() 获取更多信息。
https.Server 类#
这个类是 tls.Server 的子类,跟 http.Server 一样触发事件。查看http.Server 获取更多信息。
server.close([callback])#
callback<Function>
详见 HTTP 模块的 server.close() 方法。
server.listen()#
开启监听加密连接的HTTPS服务器。
方法与net.Server的server.listen()同。
server.maxHeadersCount#
- <number> Default:
2000
See http.Server#maxHeadersCount.
server.setTimeout([msecs][, callback])#
msecs<number> 默认值是 120000 (2 分钟).callback<Function>
server.timeout#
- <number> 默认值是 120000 (2 分钟).
server.keepAliveTimeout#
- <number> 默认值是 5000 (5 秒钟).
查看
http.Server#keepAliveTimeout.
https.createServer([options][, requestListener])#
options<Object> 接受来自tls.createServer()和tls.createSecureContext()的options.requestListener<Function> 添加到request事件的监听器.
例子:
// 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);
https.get(options[, callback])#
options<Object> | <string> | <URL> 接受与https.request()相同的options,method始终设置为GET.callback<Function>
类似 http.get(),但是用于 HTTPS。
参数 options 可以是一个对象、或字符串、或 URL 对象。
如果参数 options 是一个字符串, 它自动被 url.parse() 所解析。
如果它是一个URL 对象, 它会被自动转换为一个普通的 options 对象.
例子:
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#
https.Agent 的全局实例,用于所有 HTTPS 客户端请求。
https.request(options[, callback])#
-
options<Object> | <string> | <URL> Accepts alloptionsfromhttp.request(), with some differences in default values:protocolDefaults tohttps:portDefaults to443.agentDefaults tohttps.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) => {
// ...
});
使用 URL 作为options 的例子:
const { URL } = require('url');
const options = new URL('https://abc:xyz@example.com');
const req = https.request(options, (res) => {
// ...
});