一·path 路径模块
1.1 什么是 path 路径模块
path 模块是 Node.js 官方提供的、用来处理路径的模块。它提供了一系列的方法和属性,用来满足用户对路径的处理需求。
例如:
- path.join() 方法,用来将多个路径片段拼接成一个完整的路径字符串
- path.basename() 方法,用来从路径字符串中,将文件名解析出来
如果要在 JavaScript 代码中,使用 path 模块来处理路径,则需要使用如下的方式先导入它:
const path = require('path');
1.2 路径拼接
1. path.join() 的语法格式
使用 path.join() 方法,可以把多个路径片段拼接为完整的路径字符串,语法格式如下:
path.join([...paths])
2. path.join() 的代码示例
const path = require('path');
const fs = require('fs');
const pathStr = path.join('/a', '/b/c', '../', '/d', '/e');
console.log(pathStr);
// 实例
fs.readFile(path.join(__dirname, './files/1.text'), 'utf8', function(err, dataStr) {
if (err) {
return console.log('文件读取失败!' + err.message);
}
console.log('文件读取成功!' + dataStr);
})
1.3 获取路径中的文件名
1. path.basename() 的语法格式
使用 path.basename() 方法,可以获取路径中的最后一部分,经常通过这个方法获取路径中的文件名,语法格式如下:
path.basename(path[,ext]);
参数解读:
1.path 必选参数,表示一个路径的字符串
2.ext 可选参数,表示文件扩展名
3.返回: 表示路径中的最后一部分
2. path.basename() 的代码示例
使用 path.basename() 方法,可以从一个文件路径中,获取到文件的名称部分:
const path = require('path');
const fpath = '/a/b/c/index.html';
const fullName1 = path.basename(fpath);
console.log(fullName1); // index.html
const fullName2 = path.basename(fpath, '.html');
console.log(fullName2); // index
1.4 获取路径中的文件扩展名
1. path.extname() 的语法格式
使用 path.extname() 方法,可以获取路径中的扩展名部分,语法格式如下:
path.extname(path)
参数解读:
-
path 必选参数,表示一个路径的字符串
-
返回: 返回得到的扩展名字符串
2. path.extname() 的代码示例
使用 path.extname() 方法,可以获取路径中的扩展名部分:
const path = require('path');
const fpath = 'a/b/c/index.html';
const fext = path.extname(fpath);
console.log(fext); // .html
1.5 综合案例
const fs = require('fs');
const path = require('path');
// 定义正则表达式
const regStyle = /<style>[\s\S]*<\/style>/;
const regScript = /<script>[\s\S]*<\/script>/;
// 调用fs.readFile()方法读取文件
fs.readFile(path.join(__dirname, '/index.html'), 'utf8', function(err, htmlStr) {
if (err) {
return console.log('文件读取失败!' + err.message);
}
// 文件读取成功,调用对应方法分解出 css , js ,html
resolveCSS(htmlStr);
resolveJS(htmlStr);
resolveHTML(htmlStr);
});
function resolveCSS(dataStr) {
// 使用正则提取需要的内容
const r1 = regStyle.exec(dataStr);
// 将提取出来的样式字符串,进行字符串的 replace 替换操作
const newCSS = r1[0].replace('<style>', '').replace('</style>', '');
// 将提取出来的新字符串,使用 fs.writeFile() 方法写入到 clock 文件中的 index.css
fs.writeFile(path.join(__dirname, './clock/index.css'), newCSS, function(err) {
if (err) return console.log('index.css写入文件失败!' + err.message);
console.log('写入index.css文件成功!');
});
};
function resolveJS(dataStr) {
const r2 = regScript.exec(dataStr);
const newJS = r2[0].replace('<script>', '').replace('</script>', '');
fs.writeFile(path.join(__dirname, './clock/index.js'), newJS, function(err) {
if (err) return console.log('index.js写入文件失败!' + err.message);
console.log('写入文件index.js成功!');
});
};
function resolveHTML(dataStr) {
const newHTML = dataStr
.replace(regStyle, '<link rel="stylesheet" href="./index.css"/>')
.replace(regScript, '<script src="./index.js"></script>');
fs.writeFile(path.join(__dirname, './clock/index.html'), newHTML, err => {
if (err) return console.log('index.html文件写入失败!' + err.message);
console.log('写入文件index.html成功!');
});
};
PostScript(js函数复习):
定义和用法:
exec() 方法用于检索字符串中的正则表达式的匹配。
语法:
RegExpObject.exec(string)
参数 | 描述 |
---|---|
string | 必需。要检索的字符串。 |
返回值:
返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。
详情:https://www.w3school.com.cn/js/jsref_exec_regexp.asp