Path模块中常用函数的使用是怎样的
Admin 2022-08-18 群英技术资讯 947 次浏览
这篇文章主要讲解了“Path模块中常用函数的使用是怎样的”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Path模块中常用函数的使用是怎样的”吧!官网地址:https://nodejs.org/api/path.html
参数[...paths]: <String> 参数是一个路径序列或路径片段功能:该函数将一个路径序列或路径片段组合成一个绝对路径;
path.resolve([path1][, path2][, ...]) 从右向左依次拼接该路径序列,直到构成一个绝对路径。例如,输入参数:/foo, /bar, baz, 调用函数path.resolve('/foo', '/bar', 'baz')后返回结果是 /bar/baz;
如果处理完所有参数仍然没有构成一个绝对路径,就使用当前工作目录的绝对路径;结果返回的路径是经normalized后的,尾随斜线是没有的,除非是根路径;
Zero-length path segments are ignored.
如果路径序列中没有可用的路径片段,该函数将返回当前工作目录的绝对路径;
例子:
path.resolve('/foo/bar', './baz')
// Return: '/foo/bar/baz'
path.resolve('/foo/bar', '/tmp/file/')
// Return: '/tmp/file'
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
//如果当前工作目录是/home/myself/node,
//返回结果是: '/home/myself/node/wwwroot/static_files/gif/image.gif'
如果有任何参数不是字符串,将会抛出TypeError错误;
...paths <String> A sequence of path segmentsThe path.join() method joins all given path segments together using the platform specific separator as a delimiter, then normalizes the resulting path.
作用:该函数使用指定分隔符将参数中所有路径片段连接到一起,并返回normalize后的结果路径。
Zero-length path segments are ignored. If the joined path string is a zero-length string then '.'will be returned, representing the current working directory.
如果连接的参数长度为0字符串,将会返回'.' , 表示当前工作目录
例子:
1 path.join('/foo', 'bar', 'baz/asdf', 'quux', '..') 2 //Return: '/foo/bar/baz/asdf' 3 4 path.join('foo', {}, 'bar') 5 //throws TypeError:Arguments to path.join must be strings
如果参数中有任何路径片段不是字符串,将会抛出TypeError错误;
path <String> The path.normalize() method normalizes the given path, resolving '..' and '.' segments.
作用:标准化路径,处理'..'和'.'
When multiple, sequential path segment separation characters are found (e.g. / on POSIX and \on Windows), they are replaced by a single instance of the platform specific path segment separator. 后缀分隔符将保留;
If the path is a zero-length string, '.' is returned, representing the current working directory.
如果path是长度为0的字符串,将会返回'.' , 表示当前工作目录
For example on POSIX:
1 path.normalize('/foo/bar//baz/asdf/quux/..') 2 // Returns: '/foo/bar/baz/asdf'
On Windows:
1 path.normalize('C:\\temp\\\\foo\\bar\\..\\') 2 //Returns: 'C:\\temp\\foo\\'
pathObject <Object>
dir <String> root <String> base <String> name <String> ext <String> The path.format() method returns a path string from an object. This is the opposite ofpath.parse().
When providing properties to the pathObject remember that there are combinations where one property has priority over another:
pathObject.root is ignored if pathObject.dir is providedpathObject.ext and pathObject.name are ignored if pathObject.base existsFor example, on POSIX:
1 // If `dir`, `root` and `base` are provided, 2 // `${dir}${path.sep}${base}` 3 // will be returned. `root` is ignored. 4 path.format({ 5 root: '/ignored', 6 dir: '/home/user/dir', 7 base: 'file.txt' 8 }); 9 // Returns: '/home/user/dir/file.txt' 10 11 // `root` will be used if `dir` is not specified. 12 // If only `root` is provided or `dir` is equal to `root` then the 13 // platform separator will not be included. `ext` will be ignored. 14 path.format({ 15 root: '/', 16 base: 'file.txt', 17 ext: 'ignored' 18 }); 19 // Returns: '/file.txt' 20 21 // `name` + `ext` will be used if `base` is not specified. 22 path.format({ 23 root: '/', 24 name: 'file', 25 ext: '.txt' 26 }); 27 // Returns: '/file.txt'
On Windows:
1 path.format({ 2 dir : "C:\\path\\dir", 3 base : "file.txt" 4 }); 5 // Returns: 'C:\\path\\dir\\file.txt'
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
vue中路由自动导入的方式是什么?vue中导入路由是很基础的操作,本文给大家分享的是vue中路由自动导入的方法,下文还有示例供大家参考,感兴趣的朋友就继续往下看吧。
本文主要介绍了node连接mysql查询事务处理的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在vue开发中,难免遇到各种表单校验,下面这篇文章主要给大家介绍了关于vue表单验证rules及validator验证器使用的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
这篇文章主要介绍了vue插值表达式和v-text指令的区别,{{}}这种语法叫做插值表达式,在插值表达式中可以写任何合法的js表达式,下面来看看文章是怎么介绍该内容的吧,需要的朋友可以参考一下
这篇文章主要介绍了vue3.x中apollo的使用,通过前端自身直接获取到apollo的配置目前看到官方支持的客户端是没有vue的,本文给大家介绍了前端获取到apollo数据的过程,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008