How to pass a Buffer as argument of fs.createReadStream

后端 未结 3 2159
南旧
南旧 2021-02-18 15:53

According to docs https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options

fs.createReadStream() can accept Buffer as first argument

my node code:

3条回答
  •  一整个雨季
    2021-02-18 16:46

    @jfriend00 has already provided a very clear explanation on this issue. If a Buffer object is passed as argument to fs.createReadStream(), it should indicate the file path, not file content. As @Littlee asked in comment, here is an example code:

    var express = require('express');
    var router = express.Router();
    var fs = require('fs')
    
    router.get('/test', function(req, res) {
      var buf = Buffer.from('./test.html');
      fs.createReadStream(buf).pipe(res);
    });
    

    Please note the Buffer buf indicates a file path ("./test.html"), not the file test.html's content.

提交回复
热议问题