My goal is to construct a simple filesystem caching system for reducing the number of calls we need to make to an API for thumbnail images. The process is to check if the image already exists on the filesystem fs.stat
and if not request
the image from an API endpoint while at the same time writing the image to the file system. I was hoping I could pipe the request to both the file system and the response at the same time, but I don't believe that is possible so I first stream the response to the file system and then create a stream to pipe the image from the file system to the response
object.
我的目标是构建一个简单的文件系统缓存系统,以减少我们需要对缩略图图像的API进行调用的次数。该过程是检查文件系统fs.stat上是否已存在图像,如果没有从API端点请求图像,同时将图像写入文件系统。我希望我可以同时将请求传递给文件系统和响应,但我不相信这是可能的,所以我首先将响应流式传输到文件系统,然后创建一个流来管理图像来自文件系统到响应对象。
It's working well, but I have to believe that is a more efficient/optimized way to do this task in node.js. Any thoughts?
它运行良好,但我必须相信这是在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 个解决方案
#1
5
You can use a ThroughStream to accomplish this without having to wait for the entire file to be written to your file system. This works because the ThroughStream will internally buffer the data being piped into it.
您可以使用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);
}
})
}
#1
5
You can use a ThroughStream to accomplish this without having to wait for the entire file to be written to your file system. This works because the ThroughStream will internally buffer the data being piped into it.
您可以使用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);
}
})
}