JS在线运行

版本:

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
交互输入
极速运行模式,更高效的运行,点击编辑器上方的运行按钮即刻体验吧。
以下是用户最新保存的代码
生成指定范围的随机数 发布于:2023-03-23 16:06 指尖mud 发布于:2023-03-22 22:08 easyui方法扩展 发布于:2023-03-22 17:20 汇总计算,并输出比对结果 发布于:2023-03-20 17:40 JS触发Angular数据更新 发布于:2023-03-20 11:28 简易 sort 封装 发布于:2023-03-18 11:43 找出数组 arr 中重复出现过的元素(不用考虑返回顺序) 发布于:2023-03-17 14:27 使用JS判断一个字符串中重复出现次数最多的字符 发布于:2023-03-16 17:06 测试生成function 发布于:2023-03-16 09:43 原生js使用Cookie 发布于:2023-03-15 16:16 js时间日期 发布于:2023-03-15 15:12 js年月日 发布于:2023-03-15 15:05 循环打印输出双倍值 发布于:2023-03-15 09:51 风向util 发布于:2023-03-14 18:44 寻找100-200之间的素数 发布于:2023-03-13 16:43 阿拉伯数字转中文数字 发布于:2023-03-13 13:44 javascript snippets 发布于:2023-03-12 14:53 promise控制并发数量 发布于:2023-03-12 14:00 javascript 代码测试 发布于:2023-03-12 10:52 测试promise用法 发布于:2023-03-11 14:12 手撕promise 发布于:2023-03-11 10:37 格式化时间成时分秒的格式,前面补零 发布于:2023-03-10 07:33 搜索二维矩阵 II 发布于:2023-03-09 20:57 获取数字下标 发布于:2023-03-09 09:08 根据id 和 parentid 将数组数据转成嵌套结构 发布于:2023-03-08 16:03 手写 防抖 接口 递归深拷贝 发布于:2023-03-08 09:25 判断点是否在多边形内 发布于:2023-03-07 21:13 diffDate优化 发布于:2023-03-06 14:28 js得到两个时间的时间差 发布于:2023-03-06 13:58 js计算相差多少天 发布于:2023-03-06 14:01 归并排序算法 发布于:2023-03-03 16:39 无限循环没有图片 发布于:2023-03-03 10:09 二分搜索法 发布于:2023-03-02 16:37 测试项目 代码 发布于:2023-03-01 14:13 拼接对象键值为字符串,使用指定符号拼接 发布于:2023-02-28 14:28 正则捕捉pre,插入button 发布于:2023-02-27 21:03 测试map、forEach 发布于:2023-02-27 20:19 JS 手写题测试代码 发布于:2023-02-26 16:54 判断一个字符串中出现次数最多的字符并统计次数 发布于:2023-02-24 15:27 js 订阅消息时间 发布于:2023-02-23 22:10 nodejs基础能力测试 发布于:2023-03-21 11:21 nodejs基础能力测试 发布于:2023-02-23 12:28 Promise简易实现 发布于:2023-02-23 15:59 函数参数是否是传引用地址还是新创建对象 发布于:2023-02-22 11:15 JavaScript some不会对空数组进行检测 发布于:2023-02-21 10:37 call, bind , apply 发布于:2023-02-20 18:12 算法学习之二叉树递归遍历 发布于:2023-02-20 14:31 递归返回新数组 发布于:2023-02-17 10:04 将数据树形结构化 发布于:2023-02-16 21:19 数据结构排序 发布于:2023-02-16 20:24 [更多]
显示目录

HTTPS



HTTPS

稳定性: 3 - 稳定

HTTPS是什么?HTTPS是基于TLS/SSL的HTTP协议,在Node.js里它可以作为单独的模块来实现。在本文中,你将了解HTTPS的使用方法。

类: https.Server

https.Server是tls.Server的子类,并且和http.Server一样触发事件。

server.setTimeout(msecs, callback)

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])

server.close([callback])

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 : 描述要使用或排除的密码的字符串
  • 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.globalAgent

所有HTTPS客户端请求的 https.Agent 全局实例。

由JSRUN为你提供的JS在线运行、在线编译工具
        JSRUN提供的JS 在线运行,JS 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。