// 提供静态文件服务
function serverStatic(response, cache, absPath) {
// 检查文件是否缓存在内存中
if (cache[absPath]) {
// 从内存中返回文件
sendFile(response, absPath, cache[absPath]);
} else {
// 检查文件是否存在
fs.exist(absPath, function (exist) {
if (exist) {
// 从硬盘中读取文件
fs.readFile(absPath, function (err, data) {
if (err) {
send404(response);
} else {
cache[absPath] = data;
// 从硬盘中读取文件并返回
sendFile(response, absPath, data);
}
});
} else {
// 返回404响应
send404(response);
}
});
}
}
想问的是:if (cache[absPath]) 这句话怎么理解,cache 是空对象{},absPath是绝对路径,cache()我查了没查到具体的解释, if里面的表达式怎么理解呢,最好详细,谢谢
cache是个hash map, abspath作为key,先看看有没有cache,有就返回cache了的数据,不行就只能尝试从外部存储读数据返回,顺便放到cache里。