node.js - Node相关问题,if (cache[absPath]) 语句不懂

查看:122
本文介绍了node.js - Node相关问题,if (cache[absPath]) 语句不懂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

// 提供静态文件服务
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里。

这篇关于node.js - Node相关问题,if (cache[absPath]) 语句不懂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆