I need to handle some request without content-type
as binary file
const app = express();
app.use(bodyParser.raw({type: (req) => !req.headers['content-type'], limit: '500mb' }));
those file can be huge (eg. 500 MB).
I want to read req.body
as stream for don't wast memory, but bodyParser.raw()
make req.body
as Buffer
.
How handle req.body
as Stream
?
You can use stream to handle huge file.
Express http request is a readable stream, you can pipe the request binary to file, but make sure the output is also a writable stream.
Example code:
const fs = require('fs');
const path = require('path');
const express = require('express');
const app = express();
app.post('/', (req, res, next) => {
req.pipe(fs.createWriteStream(path.join('./uploadFiles', Date.now().toString() + '.mp4')));
req.on('end', () => {
res.end('Upload complete');
next();
})
})
app.listen('3000', () => {
console.log('Server listen to port 3000');
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.