请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Node.js HTTPS

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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_FORMAT
  • rejectUnauthorized: 如为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全局实例。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Node.js 控制台发布时间:2022-01-22
下一篇:
Node.js Smalloc发布时间:2022-01-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap