- 第1节:Node.js 教程
- 第2节:Node.js 安装配置
- 第3节:Node.js 创建第一个应用
- 第4节:Node.js NPM 使用介绍
- 第5节:Node.js REPL(交互式解释器)
- 第6节:Node.js 回调函数
- 第7节:Node.js 事件循环
- 第8节:Node.js EventEmitter
- 第9节:Node.js Buffer(缓冲区)
- 第10节:Node.js Stream(流)
- 第11节:Node.js 模块系统
- 第12节:Node.js 函数
- 第13节:Node.js 路由
- 第14节:Node.js 全局对象
- 第15节:Node.js 常用工具util
- 第16节:Node.js 文件系统
- 第17节:Node.js GET/POST请求
- 第18节:Node.js 工具模块
- 第19节:Node.js Web 模块
- 第20节:Node.js Express 框架
- 第21节:Node.js RESTful API
- 第22节:Node.js 多进程
- 第23节:Node.js JXcore 打包
- 第24节:Node.js 连接 MySQL
- 第25节:Node.js 连接 MongoDB
- 第26节:关于 Node.js
- 第27节:Node.js 示例
- 第28节:Node.js 断言测试
- 第29节:Node.js 事件
- 第30节:Node.js Punycode
- 第31节:Node.js Buffer
- 第32节:Node.js 文件系统
- 第33节:Node.js Query Strings
- 第34节:Node.js C/C++ 插件
- 第35节:Node.js 逐行读取
- 第36节:Node.js 全局对象
- 第37节:Node.js 子进程
- 第38节:Node.js REPL
- 第39节:Node.js HTTP
- 第40节:Node.js 集群
- 第41节:Node.js Smalloc
- 第42节:Node.js HTTPS
- 第43节:Node.js 控制台
- 第44节:Node.js 模块
- 第45节:Node.js 加密
- 第46节:Node.js 流
- 第47节:Node.js 网络
- 第48节:Node.js 调试器
- 第49节:Node.js 字符串解码器
- 第50节:Node.js 系统
- 第51节:Node.js DNS
- 第52节:Node.js 定时器
- 第53节:Node.js 路径
- 第54节:Node.js 域
- 第55节:Node.js TLS/SSL
- 第56节:Node.js 进程
- 第57节:Node.js TTY
- 第58节:Node.js UDP/Datagram
- 第59节:Node.js URL
- 第60节:Node.js 实用工具
- 第61节:Node.js 虚拟机
- 第62节:Node.js ZLIB
Node.js HTTPS
稳定性: 3 - 稳定
HTTPS是什么?HTTPS是基于TLS/SSL的HTTP协议,在Node.js里它可以作为单独的模块来实现。在本文中,你将了解HTTPS的使用方法。
类: https.Server
https.Server是tls.Server
的子类,并且和http.Server
一样触发事件。更多信息参见http.Server
。
server.setTimeout(msecs, callback)
详情参见http.Server#setTimeout().
server.timeout
详情参见http.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])
详情参见http.listen()。
server.close([callback])
详情参见http.close()。
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
: 描述要使用或排除的密码的字符串,格式请参阅http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMATrejectUnauthorized
: 如为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.request()。
https.globalAgent
所有HTTPS客户端请求的https.Agent全局实例。