稳定性: 3 - 稳定https是什么?https是基于tls/ssl的http协议,在node.js里它可以作为单独的模块来实现。在本文中,你将了解https的使用方法。
https.server是tls.server的子类,并且和http.server一样触发事件。更多信息参见http.server。
详情参见http.server#timeout.
返回一个新的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);详情参见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);
});option参数具有以下的值:
host: 请求的服务器域名或ip地址,默认:'localhost'hostname: 用于支持url.parse()。hostname优于hostport: 远程服务器端口。默认: 443。method: 指定http请求方法。默认:'get'。path: 请求路径。默认:'/'。如果有查询字符串,则需要包含。比如'/index.html?page=12'headers: 包含请求头的对象auth: 用于计算认证头的基本认证,即user:passwordagent: 控制agent的行为。当使用了一个agent的时候,请求将默认为connection: keep-alive。可能的值为:undefined (default): 在这个主机和端口上使用[global agent][]agent object: 在agent中显式使用passed.false: 选择性停用连接池,默认请求为:connection: closetls.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) {
...
}和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对象,和http.agent类似。详情参见https.request()。
所有https客户端请求的https.agent全局实例。