node path html模块,Node核心API的path模块学习

在webpack配置中经常会碰到path.resolve(__dirname, 'dist')、path.parse(file.path)、path.resolve(filepath)等关于path的API使用,是时候总结整理学习一下关于文件路径及path模块的API。

node中的路径分类

node中的路径大致分5类,dirname,filename,process.cwd(),./,../,其中dirname,filename,process.cwd()绝对路径。console.log(__dirname);

//F:\webgames\site20190702

console.log(__filename);

//F:\webgames\site20190702\gulpfile.js

console.log(process.cwd());

//F:\webgames\site20190702

console.log(path.resolve('./'));

//F:\webgames\site20190702__dirname: 获得当前执行文件所在目录的完整目录名

__filename: 获得当前执行文件的带有文件名的完整绝对路径

process.cwd(): 获得当前执行node命令时候的文件夹目录名

./: 不使用require时候,./与process.cwd()一样,使用require时候,与__dirname一样

只有在 require() 时才使用相对路径(./, ../) 的写法,其他地方一律使用绝对路径,如下:// 当前目录下

path.dirname(__filename) + '/path.js';

// 相邻目录下

path.resolve(__dirname, '../regx/regx.js');

path模块

path模块提供了用于处理文件和目录路径的实用API,常用的方法如下:

path.normalize(path);

该方法将非标准路径字符串转换为标准路径字符串,在转换过程中执行如下处理:解析路径字符串中的 '..' 字符串与 '.' 字符串,返回解析后的标准路径。

将多个斜杠字符串转换为一个斜杠字符串,比如将 '\\' 转换为 '\'。

将windows操作系统中的反斜杠字符串转换为正斜杠字符串。

如果路径字符串以斜杠字符串结尾,则在转换后的完整路径字符串末尾保留该斜杠字符串。

在该方法中,使用一个参数path,该参数值为需要被转换的路径字符串。该方法返回被转换后的路径字符串。const path = require('path');

const myPath = path.normalize('.//a//b//d//../e//..//');

console.log(myPath); // 输出 a/b/

path.basename(path [,ext])

该方法用于获取一个路径中的文件名,使用两个参数,path参数为必须的参数,它必须为一个文件的完整路径,可以是相对路径,也可以是一个绝对路径。

ext是可选参数,该参数的作用是在方法返回的文件名中去除该文件的扩展名。const path = require('path');

// 默认返回文件名 index.html

const a = path.basename('/a/b/c/d/index.html');

console.log(a); // 输出 index.html

// 返回index.html后,去除.html扩展名,因此会返回 index

const b = path.basename('./a/b/c/d/index.html', '.html');

console.log(b); // 输出 index

// 返回index.html后,去除html的扩展名,因此会返回 index.

const c = path.basename('./a/b/c/d/index.html', 'html');

console.log(c); // 输出 index.

// 如果扩展名不存在的话,什么都不去除

const d = path.basename('./a/b/c/d/index.html', 'ejx');

console.log(d); // 输出 index.html

path.dirname(path)

该方法用于获取一个路径中的目录名,参数值为一个路径:可以是相对路径、绝对路径、也可以为一个目录的路径、也可以是一个文件的路径。当参数值为目录的路径时:该方法返回该目录的上层目录。

当参数值为文件路径时:该方法返回该文件所在的目录。const path = require('path');

// 指定相对目录路径

const a = path.dirname('./a/b/c/d');

console.log(a); // 输出 ./a/b/c

// 指定相对文件路径

const b = path.dirname('./a/b/c/d/message.txt');

console.log(b); // 输出 ./a/b/c/d

// 指定绝对目录路径

const c = path.dirname('/a/b/c/d');

console.log(c); // 输出 /a/b/c

// 指定绝对文件路径

const d = path.dirname('/a/b/c/d/message.txt');

console.log(d); // 输出 /a/b/c/d

path.extname(path)

该方法用于获取一个路径中的扩展名,参数path必须为一个文件的完整路径,可以为相对路径,也可以为绝对路径,在该参数值中指定文件的扩展名(以'.'开始,只取最后一个.),当参数值中指定的文件没有指定扩展名时,会返回一个空字符串。const path = require('path');

const a = path.extname('/a/index.html');

console.log(a); // 输出 '.html'

const b = path.extname('/a/index.');

console.log(b); // 输出 '.'

const c = path.extname('/a/index');

console.log(c); // 输出 ''

path.extname('.index.md');

// 输出 '.md'

path.parse(path)path.parse('/home/user/dir/file.txt');

// { root: '/',

//   dir: '/home/user/dir',

//   base: 'file.txt',

//   ext: '.txt',

//   name: 'file' }

path.parse返回的是一个对象:root:代表根目录

dir:代表文件所在的文件夹

base:代表整个文件名及后缀名

name:代表文件名

ext: 代表文件的后缀名

path.format(pathObject)

path.format()方法从对象返回路径字符串。这与之相反的是path.parse()。

PathObject提供属性时,请记住,存在一个属性优先于另一个属性的组合:如果提供pathObject.dir,则忽略pathObject.root

如果pathObject.base存在,则忽略pathObject.ext和pathObject.name// If `dir`, `root` and `base` are provided,

// `${dir}${path.sep}${base}`

// will be returned. `root` is ignored.

path.format({

root: '/ignored',

dir: '/home/user/dir',

base: 'file.txt'

});

// Returns: '/home/user/dir/file.txt'

// `root` will be used if `dir` is not specified.

// If only `root` is provided or `dir` is equal to `root` then the

// platform separator will not be included. `ext` will be ignored.

path.format({

root: '/',

base: 'file.txt',

ext: 'ignored'

});

// Returns: '/file.txt'

// `name` + `ext` will be used if `base` is not specified.

path.format({

root: '/',

name: 'file',

ext: '.txt'

});

// Returns: '/file.txt'

path.join([path1], [path2], [...])

该方法将多个参数值字符串结合为一个路径字符串。path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');

// Returns: '/foo/bar/baz/asdf'

path.join('foo', {}, 'bar');

// Throws 'TypeError: Path must be a string. Received {}'传入的参数是字符串的路径片段,可以是一个,也可以是多个

返回的是一个拼接好的路径,但是根据平台的不同,他会对路径进行不同的规范化,举个例子,Unix系统是/,Windows系统是\,那么你在两个系统下看到的返回结果就不一样。

如果返回的路径字符串长度为零,那么他会返回一个.,代表当前的文件夹。

如果传入的参数中有不是字符串的,那就直接会报错

path.relative(from,to)path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');

// Returns: '../../impl/bbb'

path.relative()方法返回从相对路径from来to基于当前的工作目录。如果from、to指向同个路径,那么,返回空字符串。

如果from、to中任一者为空,那么,返回当前工作路径。

path.resolve([... paths])path.resolve('/foo/bar', './baz');

// Returns: '/foo/bar/baz'

path.resolve('/foo/bar', '/tmp/file/');

// Returns: '/tmp/file'

path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');

// If the current working directory is /home/myself/node,

// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'

path.resolve()方法将一系列路径或路径段解析为绝对路径。

给定的路径序列是从右到左进行处理的,每个后续路径都是预先设置的,直到构造出绝对路径为止。例如,给定路径段的序列:/foo,/bar,baz,调用path.resolve(“/foo”,“/bar”,“baz”)将返回/bar/baz。

如果在处理完所有给定的路径段之后,还没有生成绝对路径,则使用当前工作目录。

除非将路径解析为根目录,否则结果路径将被规范化并删除尾随斜杠。

忽略零长度路径段。

如果没有传递路径段,path.resolve()将返回当前工作目录的绝对路径。

更多关于path的api请至官网地址查看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值