|
|
| 第173行: |
第173行: |
| |} | | |} |
|
| |
|
| == stream == | | == xxxx == |
| 流分为四类:可写、可读、双工和转换。
| |
| === 双工流 ===
| |
| === 直通流 ===
| |
| PassThrough
| |
| | |
| === 获取流中的数据 ===
| |
| {{leftrightflex|<syntaxhighlight lang="javascript">
| |
| const content = await Promise.race(urls.map(host => this.fetch(
| |
| `${host}/ajax/libs/${encodeURI(relativeFilePath)}`
| |
| ).then(res => res.body))).catch(err => err);
| |
| const streamToString = (stream) => {
| |
| const chunks = [];
| |
| return new Promise((resolve, reject) => {
| |
| stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
| |
| stream.on('error', (err) => reject(err));
| |
| stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
| |
| });
| |
| };
| |
| const content_str = await this.streamToString(content);
| |
| </syntaxhighlight>|{{consoleout|<pre>
| |
| // console.log(content)
| |
| PassThrough {_readableState: ReadableState, _events: {…}, _eventsCount: 5,...
| |
| // console.log(content_str)
| |
| '/*!\n * Vue.js v2.6.14\n * (c) 2014-2021 Evan You\n * Released under the MIT License.\n */\n!function(e,t){"object"=...
| |
| </pre>}}}}
| |
| | |
| 其他写法。
| |
| <syntaxhighlight lang="javascript">
| |
| async function streamToString(stream) {
| |
| // lets have a ReadableStream as a stream variable
| |
| const chunks = [];
| |
| for await (const chunk of stream) {
| |
| chunks.push(Buffer.from(chunk));
| |
| }
| |
| return Buffer.concat(chunks).toString("utf-8");
| |
| }</syntaxhighlight>
| |
| | |
| === other ===
| |
| ==== 逐行流式传输 ====
| |
|
| |
|
| == 文件 == | | == 文件 == |
返回上一级
引言
在 Java、php 等服务端语言中,每一个访问的客户端会被单独分配一个线程,每个线程占用一定内存,以 java 为例,在 jdk1.5+ 版本,每个线程默认被分配 1m 内存(可通过 -Xss 参数设定)。
安装
安装 过程。
v8
v8
对象
事件
监听者
const events = require('events');
const eventEmitter = new events.EventEmitter();
教程
实例
实现
特点
基于无阻塞、事件驱动建立的Node服务,具有内存消耗低的优点,非常适合处理海量的网络请求。
| event-driven |
non-blocking |
示例
|
| 示例 |
示例 |
示例
|
| 示例 |
示例 |
示例
|
CPU 密集
I/O 密集型的业务场景下,使用 child_process、cluster 优化业务。
问题
组件
框架
搜索
ElasticSearch
包管理
版本管理
关键词
架构
| BFF |
示例 |
示例
|
| 示例 |
示例 |
示例
|
| 示例 |
示例 |
示例
|
渲染模式
error
代码检查
测试框架
node 进程
node 进程
child_process
xxxx
文件
RIFF
资源互换文件格式(ResourcesInterchange FileFormat)
单位换算
base64 - kb
模板:Leftrightflex
线程
思考
- 高效利用 cpu 等物理资源
- 进程和线程的健壮性、稳定性
- 高性能瓶颈
场景
参考