Egg.js构建一个stream流式接口服务


字数:391 阅读时长:1分钟 阅读:85

经常需要用到 stream 流式接口服务,比如:大文件下载、日志实时输出等等。本文将介绍如何使用Egg.js构建一个 stream 流式接口服务。

Egg.js Stream API

一、准备工作

目录结构:

1
2
3
4
5
app/
/controller
index.js
test.txt
test.sh
  1. index.js 控制器
  2. test.txt 测试文件,最好是20M以上的文件,这样才能看出流式返回的效果
  3. test.sh 测试脚本,用于实时输出日志的测试脚本

二、流式文件处理

  1. controller/index.js 文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
'use strict';

const Controller = require('egg').Controller;
const { createReadStream } = require('fs');
const { join } = require('path');

class HomeController extends Controller {

async testStream() {
const { ctx } = this;
ctx.set('Content-Type', 'text/plain; charset=utf-8');
const stream = createReadStream(join(__dirname, './test.txt'));
ctx.body = stream;
}

}

module.exports = HomeController;

三、流式日志处理

  1. controller/index.js 文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
'use strict';

const Controller = require('egg').Controller;
const { createReadStream } = require('fs');
const { join } = require('path');
const { spawn } = require('child_process');

class HomeController extends Controller {

async testStream() {
ctx.set('Content-Type', 'text/plain; charset=utf-8');

const shPath = join(__dirname, './test.sh');
const stream = spawn('sh', [ shPath ]);
ctx.body = stream.stdout;
}

}

module.exports = HomeController;
  1. controller/test.sh 文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env sh

set -e

int=1
while(( $int<=10 ))
do
echo $int
sleep 2
let "int++"
done

四、测试

前端使用 fetch 方法进行测试,为什么不用 axios ?因为 axios 是基于 XMLHttpRequest 的,不支持流式接口。 具体实现请参考:前端实现 stream 流式请求


欢迎访问:天问博客

本文作者: Tiven
发布时间: 2023-08-08
最后更新: 2023-08-18
本文标题: Egg.js构建一个stream流式接口服务
本文链接: https://www.tiven.cn/p/ea3cb27e/
版权声明: 本作品采用 CC BY-NC-SA 4.0 许可协议进行许可。转载请注明出处!
欢迎留言,提问 ^_^
个人邮箱: tw.email@qq.com
notification icon
博客有更新,将会发送通知给您!