稳定性: 3 - 稳定

HTTPS 是基于 TLS/SSL 的 HTTP 协议。在 Node 里作为单独的模块来实现。

类: 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 全局实例。

Node.js HTTPS的更多相关文章

  1. Heroku + Node.js + HTTPS

    昨天把 biz-to-me 升级到支持 HTTPS 了,为此研究了一下如何让 Heroku 上跑的 Node.js 应用支持 HTTPS.我发现并没有任何文章描述这个具体的流程,只有零碎的信息,所以在 ...

  2. Node.js之路【第一篇】初识Node.js

    什么是Node.js 1.Node.js就是运行在服务端的JavaScrip. 2.Node.js是一个基于Chrome JavaScrip运行时简历的一个平台. 3.Node.js是一个非阻塞I/O ...

  3. rabbitmq, windows/linux, c/c++/node.js/golang/dotnet

    官网:http://www.rabbitmq.com/ zeromq 相当于 message backbone,而rabbitmq相当于message broker.有的应用系统中,二者并存. (1) ...

  4. aix 上搭建node.js 环境

    下载nodejs:ibm-4.4.3.0-node-v4.4.3-aix-ppc64.bin IBM已经适配最新版本的node.js  :https://developer.ibm.com/node/ ...

  5. Node.js:JavaScript脚本运行环境

    Node.js https://nodejs.org/ 2016-08-03

  6. 安装Node.js

    1.window下安装Node.js 安装git,方便使用命令行. 网址:http://www.git-scm.com/download/ 下载后直接安装即可 接着安装Node.js https:// ...

  7. 【 js 模块加载 】深入学习模块化加载(node.js 模块源码)

    一.模块规范 说到模块化加载,就不得先说一说模块规范.模块规范是用来约束每个模块,让其必须按照一定的格式编写.AMD,CMD,CommonJS 是目前最常用的三种模块化书写规范.  1.AMD(Asy ...

  8. 使用node.js 脚手架搭建Vue项目

    1.安装node.js https://nodejs.org/zh-cn/ 下载安装node.js 在命令行测试 node -v 输出版本号说明安装成功 2.使用npm更新安装cpnm npm ins ...

  9. Node.js作web服务器总结

    1.为什么Node.js用JS开发    Node.js 含有一系列内置模块,使得程序可以脱离 Apache HTTP Server 或 IIS,作为独立服务器运行. 首先,我们都清楚的是,同时接收数 ...

随机推荐

  1. 脱upx壳--初试--单步追踪

    脱upx壳--初试--单步追踪 这里的练习题目是reversing.kr 的Easy Crack 我自己用upx加壳工具给它加了个壳,由于原文件逻辑简单,所以用它来练练手 之后用到的工具是IDA和Ol ...

  2. 如何从0开发一个Atom组件

    最近用Atom写博客比较多,然后发现一个很严重的问题..没有一个我想要的上传图片的方式,比如某乎上边就可以直接copy/paste文件,然后进行上传.然而在Atom上没有找到类似的插件,最接近的一个, ...

  3. 给定了经纬度的一张my_latlng表,和一个my_grid表,怎么实现my_latlng表回mygrid中的id?

    场景: 假设我们拥有一个拥有了一系列经纬度的表my_latlng(lat string,lng string)表,还有一张给定的栅格表my_grid(gridid bigint,centerlng d ...

  4. Flask博客开发——登录验证码

    这部分为Flask博客的登录页面加个验证码.使用了PIL模块生成验证码图片,并通过Flask的session机制,进行验证码验证. 1.生成验证码 使用string模块:string.ascii_le ...

  5. 原来你是这样的Promise

    1. Promise简介 promise是异步编程的一种解决方案,它出现的初衷是为了解决回调地狱的问题. 打个比方,我需要: --(延迟1s)--> 输出1 --(延迟2s)--> 输出2 ...

  6. 盒子浮动float

    一.float的基本规律 规律1: 标准流模型中的块级盒子,默认宽度100%: 而浮动的块级盒子,宽度不会自动伸展,而是由内容(文字.padding)撑开: 浮动后的行级元素,可以设置宽度高度等属性. ...

  7. Spring-cloud(二)注册服务提供者搭建

    上文已经写了如何去搭建注册中心,仅有注册中心是远远不够的,所以我们需要注册到注册中心并提供服务的节点,这里称为注册服务提供者 前提 阅读上文,并成功搭建注册中心,环境无需改变 项目搭建 这里我们需要新 ...

  8. [机器学习Lesson4]多元线性回归

    1. 多元线性回归定义 多元线性回归也被称为多元线性回归. 我们现在介绍方程的符号,我们可以有任意数量的输入变量. 这些多个特征的假设函数的多变量形式如下: hθ(x)=θ0+θ1x1+θ2x2+θ3 ...

  9. HDU 4641 K-string

    Description Given a string S. K-string is the sub-string of S and it appear in the S at least K time ...

  10. 【Codeforces Round #431 (Div. 1) D.Shake It!】

    ·最小割和组合数放在了一起,产生了这道题目. 英文题,述大意:     一张初始化为仅有一个起点0,一个终点1和一条边的图.输入n,m表示n次操作(1<=n,m<=50),每次操作是任选一 ...