Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为createReadStream分配缓冲区_Javascript_Node.js_Typescript_Buffer_Fs - Fatal编程技术网

Javascript 如何为createReadStream分配缓冲区

Javascript 如何为createReadStream分配缓冲区,javascript,node.js,typescript,buffer,fs,Javascript,Node.js,Typescript,Buffer,Fs,根据,createReadStream可以接受缓冲区类型作为path参数 但许多问答只提供了如何通过字符串发送参数的解决方案,而不是缓冲区发送参数的解决方案 如何正确设置缓冲区参数以满足createReadStream的路径 这是我的代码: fs.access(filePath, (err: NodeJS.ErrnoException) => { // Response with 404 if (Boolean(err)) { res.writeHead(404); res

根据,createReadStream可以接受缓冲区类型作为path参数

但许多问答只提供了如何通过字符串发送参数的解决方案,而不是缓冲区发送参数的解决方案

如何正确设置缓冲区参数以满足createReadStream的路径

这是我的代码:

fs.access(filePath, (err: NodeJS.ErrnoException) => {
    // Response with 404
    if (Boolean(err)) { res.writeHead(404); res.end('Page not Found!'); return; }

    // Create read stream accord to cache or path
    let hadCached = Boolean(cache[filePath]);
    if (hadCached) console.log(cache[filePath].content)
    let readStream = hadCached
        ? fs.createReadStream(cache[filePath].content, { encoding: 'utf8' })
        : fs.createReadStream(filePath);
    readStream.once('open', () => {
        let headers = { 'Content-type': mimeTypes[path.extname(lookup)] };
        res.writeHead(200, headers);
        readStream.pipe(res);
    }).once('error', (err) => {
        console.log(err);
        res.writeHead(500);
        res.end('Server Error!');
    });

    // Suppose it hadn't cache, there is a `data` listener to store the buffer in cache
    if (!hadCached) {
        fs.stat(filePath, (err, stats) => {
            let bufferOffset = 0;
            cache[filePath] = { content: Buffer.alloc(stats.size, undefined, 'utf8') };  // Deprecated: new Buffer

            readStream.on('data', function(chunk: Buffer) {
                chunk.copy(cache[filePath].content, bufferOffset);
                bufferOffset += chunk.length;
                //console.log(cache[filePath].content)
            });
        });
    }
});

```

使用内置
库中的
直通
方法:

const stream = require("stream");

let readStream = new stream.PassThrough();
readStream.end(new Buffer('Test data.'));

// You now have the stream in readStream
readStream.once("open", () => {
    // etc
});