Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Node.js 入门系列] 本地路径 path 模块 #23

Open
webfansplz opened this issue Dec 20, 2019 · 0 comments
Open

[Node.js 入门系列] 本地路径 path 模块 #23

webfansplz opened this issue Dec 20, 2019 · 0 comments
Labels

Comments

@webfansplz
Copy link
Owner

webfansplz commented Dec 20, 2019

本地路径 path 模块

Node.js 提供了 path 模块,用于处理文件路径和目录路径 . 不同操作系统 表现有所差异 !

1. 获取路径的目录名

const path = require('path')

path.dirname('/path/example/index.js') // /path/example

2. 获取路径的扩展名

const path = require('path')

path.extname('/path/example/index.js') // .js

3. 是否是绝对路径

const path = require('path')

path.isAbsolute('/path/example/index.js') // true

path.isAbsolute('.') // false

4. 拼接路径片段

path.join('/path', 'example', './index.js') // /path/example/index.js

5. 将路径或路径片段的序列解析为绝对路径。

path.resolve('/foo/bar', './baz')
// 返回: '/foo/bar/baz'

path.resolve('/foo/bar', '/tmp/file/')
// 返回: '/tmp/file'

path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
// 如果当前工作目录是 /home/myself/node,
// 则返回 '/home/myself/node/wwwroot/static_files/gif/image.gif'

6. 规范化路径

path.normalize('/path///example/index.js') //  /path/example/index.js

7. 解析路径

path.parse('/path/example/index.js')

/*
 { root: '/',
  dir: '/path/example',
  base: 'index.js',
  ext: '.js',
  name: 'index' }
*/

8. 序列化路径

path.format({
  root: '/',
  dir: '/path/example',
  base: 'index.js',
  ext: '.js',
  name: 'index'
}) // /path/example/index.js

9. 获取 from 到 to 的相对路径

path.relative('/path/example/index.js', '/path') // ../..

上一节: [Node.js 入门系列] 事件触发器 events 模块

下一节: [Node.js 入门系列] 文件操作系统 fs 模块

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant