node.js流响应始终处于挂起状态

时间:2019-11-12 08:24:27

标签: node.js stream koa

我正在尝试通过koa发送视频流。但是当我向邮递员请求时,它就卡在了那里。

    async timesheetSummaryReport(ctx) {
            let {startDate,endDate,userId,appInfo} = ctx.request.body
            db.connect();
            let connection = db.getInstance()         
            let p = await new Promise(resolve=>{
                let result;
                const readable = require('stream').Readable
                const s = new readable;
                s.read = function (size) {};
                s.push('Date,matter Id,user Id, Duration,Cost\n')
                result = connection.query(`
                    SELECT md.Date as date,md.matter_id as matterId,m.name as matterName, c.name as clientName,g.name as groupName, duration, cost FROM matter_duration as md
                    INNER JOIN (matter as m) ON m.id=md.matter_id
                    INNER JOIN (client as c) on c.id=m.client_id 
                    INNER JOIN (\`group\` as g) on g.id = c.group_id
                    WHERE date between ? and ? and user_id= ?
                    order by matter_id;`,
                    [startDate,endDate,userId]
                )

                result.on('result',data=>{
                    console.log(Object.values(data).join())
                    s.push(Object.values(data).join() + '\n')
                })

                result.on('end',()=>{
                    console.log('All the row has been processed')
                    s.push(null)
                    resolve(s)
                })
)
            })
            console.log('The stream has ended and ready to be sent')
            ctx.body = p;
        }
    }

当我记录流时,流已结束的可读流状态,但邮递员仍在等待响应。

你能告诉我为什么吗?

0 个答案:

没有答案