1

I have a server that gets a file being uploaded from a html page and stores the file in a writable stream,

but if the file is a little big, it creates multiple buffers and i have to create an array to store all the buffers and concat them later, here is the code

app.post('/upload', (req, res) => {
    const writableStream = fs.createWriteStream('image.jpg')

    const chunks = []

    req.on('data', data => {
        chunks.push(data)
    })

    req.on('end', () => {
        const singleBuffer = Buffer.concat(chunks)

        writableStream.write(singleBuffer)

        res.status(200).json({status: "Done"})
    })
    
})

i would like to know if this is a good way of doing it or there is a better way of doing this instead of create an array to store all chunks and later write them to the stream

0 Answers0