2.3Path模块
2.3.1获取js文件的绝对路径
js
console.log(__dirname) //js文件所在的文件夹的绝对路径
console.log(__filename) //js文件的绝对路径
输出:
G:\py_project\nodejs_study
G:\py_project\nodejs_study\file.js
2.3.2拼接规范的绝对路径path.resolve()
js
path=require('path')
console.log('path:',(path.resolve(__dirname,'./html',)))
输出:
js
path: G:\py_project\nodejs_study\html
2.3.3获取操作系统路径分隔符path.sep
js
path=require('path')
console.log('路径分隔符:',path.sep)
输出:
js
路径分隔符: \
2.3.4解析路径并返回对象path.parse
js
path=require('path')
pathInfo=path.parse(__filename) //获取路径的详细信息
basename=path.basename(__filename) //获取路径中的文件名
dirname=path.dirname(__filename) //获取绝对路径
extname=path.extname(__filename) //获取文件扩展名
console.log('pathINfo:',pathInfo)
console.log('basename:',basename)
console.log('dirname:',dirname)
console.log('extname:',extname)
输出:
js
pathINfo: {
root: 'G:\\',
dir: 'G:\\py_project\\nodejs_study',
base: 'file.js',
ext: '.js',
name: 'file'
}
basename: file.js
dirname: G:\py_project\nodejs_study
extname: .js