Node.js v8.x 中文文档
目录
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.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) => {
// ...
});
- 断言测试
- 异步钩子(Async Hooks)
- 缓存(Buffer)
- C++ 插件
- C/C++ 插件 - N-API
- 子进程
- 集群(Cluster)
- 命令行参数
- 控制台(Console)
- 加密(Crypto)
- 调试器
- 废弃的 API
- DNS
- 域(Domain)
- ECMAScript 模块
- 错误(Errors)
- 事件(Events)
- 文件系统
- 全局对象(Globals)
- HTTP
- HTTP/2
- HTTPS
- 检查工具(Inspector)
- 国际化
- 模块(Modules)
- 网络(Net)
- 操作系统(OS)
- 路径(Path)
- 性能钩子(Performance Hooks)
- 进程
- Punycode
- 查询字符串
- 逐行读取
- 交互式解释器(REPL)
- 流(Stream)
- 字符串解码
- 定时器(Timers)
- 安全传输层(TLS/SSL)
- 事件跟踪(Tracing)
- TTY
- UDP / 数据报
- URL
- 工具集
- V8
- 虚拟机(VM)
- 压缩(ZLIB)