node环境下怎样优化引入多文件(实现自动化)

const mocks = [];
function getJsonFiles(jsonPath) {
    function findJsonFile(path) {
        let files = fs.readdirSync(path);
        files.forEach(function(item) {
            let fPath = join(path, item);
            let stat = fs.statSync(fPath);
            if (stat.isDirectory() === true) {
                findJsonFile(fPath);
            }
            if (stat.isFile() === true) {
                // 如果需要对指定路径进行处理
                let str = fPath.substring(0, 10);
                let newStr = fPath.replace(str, './data/');
                // 否则直接require
                mocks.push(...require(newStr));
            }
        });
    }
    findJsonFile(jsonPath);
}
getJsonFiles('./mock/data');

作者:Vam的金豆之路

主要领域:前端开发

我的微信:maomin9761

微信公众号:前端历劫之路