1.解决路径拼接的问题
在使用 fs 模块操作文件时,如果提供的操作路径是以 ./ 或 ../ 开头的相对路径时,很容易出现路径动态拼接错误的问题。
原因:代码在运行的时候,会以执行 node 命令时所处的目录,动态拼接出被操作文件的完整路径。
解决方案:在使用 fs 模块操作文件时,直接提供完整的路径,不要提供 ./ 或 ../ 开头的相对路径,从而防止路径动态拼接的问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| const fs = require('fs');
fs.readFile('./files/1.txt', 'utf8', function(err, dataStr) { if (err) { return console.log('读取文件失败!' + err.message); } console.log('读取文件成功!' + dataStr); });
fs.readFile('C:/Users/escook/Desktop/Node.js基础/day1/code/files/1.txt', 'utf8', function(err, dataStr) { if (err) { return console.log('读取文件失败!' + err.message); } console.log('读取文件成功!' + dataStr); });
console.log(__dirname);
fs.readFile(__dirname + '/files/1.txt', 'utf8', function(err, dataStr) { if (err) { return console.log('读取文件失败!' + err.message); } console.log('读取文件成功!' + dataStr); });
|
2.path 路径模块
1.path.join([…paths]): 用来将多个路径片段拼接成一个完整的路径字符串
参数:
- …paths(string):路径片段的序列
- 返回值: string
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 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.txt'), 'utf8', function(err, dataStr) { if (err) { return console.log(err.message); } console.log(dataStr); })
|
2.path.basename(path[, ext]): 获取路径中的最后一部分,经常通过这个方法获取路径中的文件名
参数:
- path: string, 必选参数,表示一个路径的字符串
- ext: string, 可选参数,表示文件扩展名
- 返回: string, 表示路径中的最后一部分
1 2 3 4 5 6 7 8 9 10 11 12
| const path = require('path')
const fpath = '/a/b/c/index.html'
const fullName = path.basename(fpath) console.log(fullName)
const nameWithoutExt = path.basename(fpath, '.html') console.log(nameWithoutExt)
|
3.path.extname(path): 可以获取路径中的扩展名部分
参数:
- path: string, 必选参数,表示一个路径的字符串
- 返回: string, 返回得到的扩展名字符串
1 2 3 4 5 6 7 8
| const path = require('path')
const fpath = '/a/b/c/index.html'
const fext = path.extname(fpath) console.log(fext)
|
3.fs模块和path模块案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| const fs = require('fs')
const path = require('path')
const regStyle = /<style>[\s\S]*<\/style>/ const regScript = /<script>[\s\S]*<\/script>/
fs.readFile(path.join(__dirname, '../素材/index.html'), 'utf8', function(err, dataStr) { if (err) return console.log('读取HTML文件失败!' + err.message) resolveCSS(dataStr) resolveJS(dataStr) resolveHTML(dataStr) })
function resolveCSS(htmlStr) { const r1 = regStyle.exec(htmlStr) const newCSS = r1[0].replace('<style>', '').replace('</style>', '') fs.writeFile(path.join(__dirname, './clock/index.css'), newCSS, function(err) { if (err) return console.log('写入 CSS 样式失败!' + err.message) console.log('写入样式文件成功!') }) }
function resolveJS(htmlStr) { const r2 = regScript.exec(htmlStr) const newJS = r2[0].replace('<script>', '').replace('</script>', '') fs.writeFile(path.join(__dirname, './clock/index.js'), newJS, function(err) { if (err) return console.log('写入 JavaScript 脚本失败!' + err.message) console.log('写入 JS 脚本成功!') }) }
function resolveHTML(htmlStr) { const newHTML = htmlStr.replace(regStyle, '<link rel="stylesheet" href="./index.css" />').replace(regScript, '<script src="./index.js"></script>') fs.writeFile(path.join(__dirname, './clock/index.html'), newHTML, function(err) { if (err) return console.log('写入 HTML 文件失败!' + err.message) console.log('写入 HTML 页面成功!') }) }
|
注意:
- fs.writeFile() 方法只能用来创建文件,不能用来创建路径
- 重复调用 fs.writeFile() 写入同一个文件,新写入的内容会覆盖之前的旧内容