首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >node.js -文件系统缓存图像和响应管道图像

node.js -文件系统缓存图像和响应管道图像
EN

Stack Overflow用户
提问于 2016-08-30 16:04:19
回答 1查看 3.4K关注 0票数 7

我的目标是构建一个简单的文件系统缓存系统,以减少我们需要对缩略图进行API调用的次数。这个过程是检查映像是否已经存在于文件系统fs.stat上,如果不是request,则检查来自API端点的映像,同时将映像写入文件系统。我希望能够同时将请求传递到文件系统和响应,但我认为这是不可能的,所以我首先将响应流流到文件系统,然后创建一个流,将图像从文件系统传输到response对象。

它运行得很好,但我必须相信这是在node.js中完成此任务的一种更有效/优化的方法。有什么想法吗?

代码语言:javascript
复制
    function (req, res, next) {

        // Check to see if the image exists on the filesystem
        // TODO - stats will provide information on file creation date for cache checking
        fs.stat(pathToFile, function (err, stats) {
            if (err) {

                // If the image does not exist on the file system
                // Pipe the image to a file and then to the response object
                var req = request.get({
                    "uri": "http://www.example.com/image.png",
                    "headers": {
                        "Content-Type": "image/png"
                    }
                });

                // Create a write stream to the file system
                var stream = fs.createWriteStream(pathToFile);
                req.pipe(stream);
                stream.on('finish', function () {
                    fs.createReadStream(pathToFile)
                        .pipe(res);
                })
            }
            else {

                // If the image does exist on the file system, then stream the image to the response object
                fs.createReadStream(pathToFile)
                    .pipe(res);
            }
        })
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-30 16:28:19

您可以使用ThroughStream来完成这一任务,而不必等待整个文件被写入文件系统。这是因为ThroughStream会在内部缓冲被管道传输到它的数据。

代码语言:javascript
复制
var stream = require('stream')
function (req, res, next) {

    // Check to see if the image exists on the filesystem
    // TODO - stats will provide information on file creation date for cache checking
    fs.stat(pathToFile, function (err, stats) {
        if (err) {

            // If the image does not exist on the file system
            // Pipe the image to a file and the response object
            var req = request.get({
                "uri": "http://www.example.com/image.png",
                "headers": {
                    "Content-Type": "image/png"
                }
            });

            // Create a write stream to the file system
            req.pipe(
              new stream.PassThrough().pipe(
                fs.createWriteStream(pathToFile)
              )
            )

            // pipe to the response at the same time
            req.pipe(res)
        }
        else {

            // If the image does exist on the file system, then stream the image to the response object
            fs.createReadStream(pathToFile)
                .pipe(res);
        }
    })
}
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39232296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档