这个req.on(‘data’)本身就是一边传一边拼接吧?那如果要使用pipe的话,要在哪个地方createReadStream呢?
能不能用stream和pipe来改写一下以前getPostData的逻辑???
//以前的代码
const getPostData = req => {
const promise = new Promise((resolve, rej) => {
if (req.method !== 'POST') {
resolve({})
return
}
if (req.headers['content-type'] !== 'application/json') {
resolve({})
return
}
let postData = ''
let chunks = []
let size = 0
req.on('data', d => {
chunks.push(d)
size += d.length
})
req.on('end', () => {
let buf = Buffer.concat(chunks, size)
postData = buf.toString()
if (!!postData) {
resolve(JSON.parse(postData))
return
}
resolve({})
})
})
return promise
}