node.js - 将图像缓存到文件系统并将图像通过管道传输到响应

Posted

技术标签:

【中文标题】node.js - 将图像缓存到文件系统并将图像通过管道传输到响应【英文标题】:node.js - Cache Image to Filesystem and Pipe Image to Response 【发布时间】:2017-01-07 00:31:57 【问题描述】:

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

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

    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);
            
        )
    

【问题讨论】:

【参考方案1】:

您可以使用 ThroughStream 来完成此操作,而无需等待将整个文件写入您的文件系统。这是可行的,因为 ThroughStream 将在内部缓冲通过管道传输到其中的数据。

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);
        
    )

【讨论】:

正是我要找的东西……一定要爱!谢谢。

以上是关于node.js - 将图像缓存到文件系统并将图像通过管道传输到响应的主要内容,如果未能解决你的问题,请参考以下文章

node.js:并排合并 2 个图像并将其保存回来

如何在 Node.js 中将文件上传到 GCloud?

我应该将图像存储在MongoDB或本地文件系统中(通过Node.js)

如何使用 node.js 获取图像上传进度并在我的 iphone 应用程序中使用它

从 url 下载文件并将其上传到 AWS S3 而不保存 - node.js

请推荐一个用于将 IPTC 数据写入图像的 Node 模块?