koa2中的如果所有中间件走完 没有设置ctx.body则会响应404
import recorders from '../../recorder';
import { IMiddleware } from 'koa-router';
import https from 'https';
const getStreamTexts: IMiddleware<
any,
{
query: {
envName: string;
streamId: string;
startIndex: string;
};
}
> = async (ctx) => {
try {
const { envName, streamId, startIndex } = ctx.query;
const streamDetail = await recorders.getStreamDetail(envName, streamId);
if (streamDetail.textOssPath) {
return await new Promise(resolve => {
https.get(streamDetail.textOssPath, res => {
resolve(ctx.body = res)
})
})
} else {
const streamTexts = await recorders.getStreamTexts(envName, streamId, Number(startIndex));
return ctx.body = {
code: 200,
texts: streamTexts
};
}
} catch (err) {
return ctx.body = {
code: 500,
data: err
};
}
}
export default getStreamTexts;
在回调函数中设置ctx.body需要用promise包起来 ,直接返回流时 不能用
ctx.body = {
code: 200,
data: res,
}
这种响应头是application/json
如果res是一个流,响应头为application/octet-stream
就只能用ctx.body = res