使用express的nodejs输入流

Ric*_*doE 3 inputstream node.js express

有没有一种方法可以使用Express路由使用者将输入流发送到端点并进行读取?

简而言之,我希望端点用户通过流式传输而不是采用多部分/表单方式上传文件。就像是:

app.post('/videos/upload', (request, response) => {
    const stream = request.getInputStream();
    const file = stream.read();
    stream.on('done', (file) => {
        //do something with the file
    });
});
Run Code Online (Sandbox Code Playgroud)

有可能做到吗?

rob*_*lep 6

在快递,request对象的增强版本http.IncomingMessage,其中“......实现了可读流接口”

换句话说,request已经是一个流:

app.post('/videos/upload', (request, response) => {
  request.on('data', data => {
    ...do something...
  }).on('close', () => {
    ...do something else...
  });
});
Run Code Online (Sandbox Code Playgroud)

如果您打算先将整个文件读到内存中(可能不是),也可以使用bodyParser.raw()

const bodyParser = require('body-parser');
...
app.post('/videos/upload', bodyParser.raw({ type : '*/*' }), (request, response) => {
  let data = req.body; // a `Buffer` containing the entire uploaded data
  ...do something...
});
Run Code Online (Sandbox Code Playgroud)