我的目标是构建一个简单的文件系统缓存系统,以减少我们需要对缩略图进行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);
}
})
}发布于 2016-08-30 16:28:19
您可以使用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);
}
})
}https://stackoverflow.com/questions/39232296
复制相似问题