Home > Web Front-end > JS Tutorial > An article explaining the usage of node path module in detail

An article explaining the usage of node path module in detail

藏色散人
Release: 2022-08-08 15:29:45
forward
1935 people have browsed it

The following are commonly used path module methods. For details, please refer to the official website: http://nodejs.cn /api/path.html

  • path.resolve([…paths])

/** 
 * 将路径或路径片段的序列解析为绝对路径(给定的路径序列从右到左进行处理,每个后续的 path 前置,直到构造出一个绝对路径)
 * @param {String} ...paths 路径或路径片段的序列
 * @return {String}
 */
 
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'
Copy after login
  • path.join([…paths])

/** 
 * 使用平台特定的分隔符作为定界符将所有给定的 path 片段连接在一起,然后规范化生成的路径
 * @param {String} ...paths 路径片段的序列
 * @return {String}
 */
 
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
// 返回: '/foo/bar/baz/asdf'

path.join('foo', {}, 'bar');
// 抛出 'TypeError: Path must be a string. Received {}'
Copy after login
  • path.extname(path )

/** 
 * 返回 path 的扩展名
 * @param {String} path
 * @return {String}
 */
 path.basename('src/pages/shop/detail.vue');// 返回: '.vue'
Copy after login
  • path.basename(path[, ext])

/** 
 * 返回 path 的最后一部分
 * @param {String} path
 * @param {String} ext 可选的文件扩展名
 * @return {String}
 */
 
path.basename('src/pages/shop/detail.vue');
// 返回: 'detail.vue'

path.basename('src/pages/shop/detail.vue', '.vue');
// 返回: 'detail'
Copy after login
  • path.dirname(path)

/** 
 * 返回 path 的目录名
 * @param {String} path
 * @return {String}
 */
 path.dirname('src/pages/shop/detail.vue');// 返回: 'src/pages/shop'
Copy after login
  • ##path.format(pathObject)

  • /** 
     * 返回 path 的目录名
     * @param {Object} pathObject
     * {
     *		dir
     *		root
     *		base
     *		name
     *		ext
     *	}
     * @return {String}
     */
     
    // 如果提供了 `dir`、 `root` 和 `base`,
    // 则返回 `${dir}${path.sep}${base}`。
    // `root` 会被忽略。
    path.format({
      root: '/ignored',
      dir: '/home/user/dir',
      base: 'file.txt'
    });
    // 返回: '/home/user/dir/file.txt'
    
    // 如果未指定 `dir`,则使用 `root`。 
    // 如果只提供 `root`,或 'dir` 等于 `root`,则将不包括平台分隔符。 
    // `ext` 将被忽略。
    path.format({
      root: '/',
      base: 'file.txt',
      ext: 'ignored'
    });
    // 返回: '/file.txt'
    
    // 如果未指定 `base`,则使用 `name` + `ext`。
    path.format({
      root: '/',
      name: 'file',
      ext: '.txt'
    });
    // 返回: '/file.txt'
    Copy after login
    【Recommended:

    node.js video tutorial

    The above is the detailed content of An article explaining the usage of node path module in detail. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template