HTTP 요청에서 오는 스트림을 처리하는 데 문제가 있습니다. 핸들러는 스트림에서 데이터를 수집하고 응답을 보내기 전에 비동기 작업을 수행합니다.
스트림 데이터가 수집되는 즉시 HTTP 연결 someAsyncStuff
이 호출 되기 직전에 닫힙니다 .
이것은 문제를 재현하는 예입니다. 내가 도대체 뭘 잘못하고있는 겁니까?
import * as Busboy from 'busboy'
import * as express from 'express'
import { pipeline } from 'stream'
import concat = require('concat-stream')
const app = express()
app.post('/', (req, res) => {
const busboy = new Busboy({ headers: req.headers })
busboy.on('file', (_, file) => {
pipeline(
file,
concat(buffer =>
someAsyncStuff(buffer.toString())
.then(length => res.send({ length }))
.catch(err => res.status(500).send(err.message))
),
err => {
if (err) res.status(500).send(err.message)
}
)
})
pipeline(req, busboy, err => {
if (err) res.status(500).send(err.message)
})
})
function someAsyncStuff(s: string): Promise<number> {
return new Promise(resolve => setTimeout(() => resolve(s.length), 1))
}
app.listen('3000')
req.pipe 사용이 작동하는 것 같습니다.
import Busboy from 'busboy'
import express from 'express'
import { pipeline } from 'stream'
import concat from 'concat-stream'
const app = express()
app.post('/', (req, res) => {
const busboy = new Busboy({ headers: req.headers })
busboy.on('file', (_, file) => {
pipeline(
file,
concat(buffer =>
someAsyncStuff(buffer.toString())
.then(length => res.json({ length }))
.catch(err => res.status(500).send(err.message))
),
err => {
if (err) res.status(500).send(err.message)
}
)
})
req.pipe(busboy);
})
function someAsyncStuff(s) {
return new Promise(resolve => setTimeout(() => resolve(s.length), 1000))
}
app.listen('3000')
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다