HTTPS是建立在TLS/SSL之上的HTTP协议。在io.js
中,它被作为单独模块实现。
这个类是tls.Server
的子类,并且和http.Server
触发相同的事件。更多信息请参阅http.Server
。
参阅http.Server#setTimeout()
。
参阅http.Server#timeout
。
返回一个新的HTTPS web服务器对象。options
与tls.createServer()
中的类似。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);
详情参阅http.listen()
。
详情参阅http.close()
。
向一个安全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);
});
options
参数有以下选项:
localhost
。host
的别名。为了支持url.parse()
的话,hostname
比host
更好些。host
和hostname
时的IP地址协议族。合法值是4
和6
。当没有指定时,将都被使用。80
。socket
(使用host:port
或socketPath
)。GET
。/
。如果有查询字符串,则需要包含。例如'/index.html?page=12'。请求路径包含非法字符时抛出异常。目前,只否决空格,不过在未来可能改变。'user:password'
。agent: 控制agent
行为。当使用一个代理时,请求将默认为Connection: keep-alive
。可能值有:
agent
中显示使用passed。agent
的连接池。默认请求为Connection: close
。以下来自tls.connect()
的选项也可以被指定。但是,一个globalAgent
会默默忽略这些。
null
。null
。null
。null
。http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT
。true
,服务器证书会使用所给的CA列表验证。验证失败时,一个error
事件会被触发。验证发生于连接层,在HTTP请求发送之前。默认为true
。SSLv3_method
强制使用SSL v3。可用的值取决你的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) {
...
}
类似于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);
});
一个与http.Agent
类似的HTTPS Agent
对象。更多信息请参阅https.request()
。
所有HTTPS客户端请求的全局https.Agent
实例。