Node.js HTTPS
稳定性: 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_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) { ... }
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 全局实例。
转载于:https://www.cnblogs.com/navysummer/p/8458746.html
Node.js HTTPS相关推荐
- node.js https 模块设置请求头等信息
// https://www.iqiyi.com/v_19rs789v28.html var fs = require('fs'); var https = require('https'); var ...
- js 弹出层的点击事件影响到底层的点击事件_聊一聊 Node.js 错误处理
个人博客:https://blog.skrskrskrskr.com 错误分类 软件程序中,我们可以将错误大致分为外部错误和内部错误两大类. 外部错误是正确编写的程序在运行时产生的错误.它并不是程序本 ...
- 使用node.js 脚手架搭建Vue项目
1.安装node.js https://nodejs.org/zh-cn/ 下载安装node.js在命令行测试 node -v 输出版本号说明安装成功 2.使用npm更新安装cpnm npm inst ...
- JavaScript教程9 - Node.js
Node.js 安装Node.js https://nodejs.org/ npm npm其实是Node.js的包管理工具(package manager). 命令行模式 执行node hello.j ...
- vscode安装node.js
VSCode是一款由微软开发的轻量级编辑器.要在VSCode上安装Node.js,需要遵循以下步骤: 在官网上下载并安装最新版本的Node.js(https://nodejs.org/en/downl ...
- Node.js 部署免费/自动续订 HTTPS
统计了使用 Chrome 浏览器,访问的站点统计中,HTTPS 使用率的增长情况: 而在今年 2 月份,Chrome 团队也宣布,将在 2018 年 7 月份发布的 Chrome 68 中,将没有部署 ...
- 日记--node.js 和nginx对比环境变量立刻生效https://www.cnblogs.com/zht-blog/p/4033951.html
Nginx的性能比Node.js的HTTP模块要好很多 但Nginx考量的是面向客户端, 后端业务方面依然是受具体业务影响,而Node.js则可以利用异步I/O来实现业务并行,以提升效率 Nginx没 ...
- 通过新浪云部署Node.js微信小程序商城(不用买域名、不用备案、不用配置https)
生产环境推荐使用阿里云服务器,阿里云代金券领取 最近更新时间:2019-03-09 原文链接:https://nideshop.com/documents/nideshop-manual/deploy ...
- 通过新浪云部署Node.js微信小程序商城(不用买域名、不用备案、不用配置https)...
本文档为微信小程序商城NideShop项目的安装部署教程(GitHub),欢迎star 一.购买新浪云SAE 为什么选择SAE?免费二级域名和支持https访问,不用备案,可用做微信小程序服务器. S ...
最新文章
- 服了!会 Python 找工作这么容易?
- JSON数据解析及gson.jar包
- bootstrap项目更改为vue_取代Jquery,用Vue 构建Bootstrap 4 应用
- 4. 用MVC实现URL路由
- ubuntu服务器创建共享文件夹,Ubuntu samba安装创建共享目录及使用
- 【AD】mm,mile,inch+电流大小同线宽关系
- Sqlserver常用函数例子说明
- Android 免费模式将终结?
- 2003服务器系统pe,SERVER 2003 PE(移动存储PE系统)v16.68免费版
- 丢失api-ms-win-crt-heap-l1-1-0.dll 错误的解决办法
- python把英语句子成分字母_有没有那种能分析英语句子成分的APP?
- 图形外部加文字 r语言_将外部图形卡连接到笔记本电脑的最佳方法
- 地面三维激光扫描仪在火灾现场调查取证中的应用
- Mac苹果电脑安装虚拟机
- 如何用ChatGPT做会议总结?
- Glide 图片闪烁问题
- The 15th Jilin Provincial Collegiate Programming Contest
- Android:高仿百度外卖、美团、淘点点二级联动效果!
- 光学瞄准镜是怎么调节校准的,你学会了吗?
- java的String类源码详解
热门文章
- 【译】Three Security Trends Are Key to Decentralize Artificial Intelligence
- Ethereum Bootstrap 以太坊本地私有链开发环境搭建
- JZOJ 3401 JZOJ 5673. 【GDOI2018Day1模拟4.20】爬山法
- 计算机应用与网络讲义,计算机基础讲义
- nodejs sqlite3_NodeJS 使用 better-sqlite3 操作sqlite 数据库
- centos7查看当前系统时间、_CentOS7.4.1708查看系统相关信息及系统的初步优化
- python中的seth函数_Python入门——turtle库的使用
- 20201010《近代数学》第1节课 笔记
- numberformatexception是什么异常_译文《最常见的10种Java异常问题》
- gamma校正_什么是Gamma校正?