2024-07-08
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
In Node.js,path
Modules are used to process and convert file paths. The following are some commonly usedpath
Module methods and their descriptions:
path.basename(path[, ext])
const path = require('path');
console.log(path.basename('/foo/bar/baz/asdf/quux.html')); // 输出: 'quux.html'
console.log(path.basename('/foo/bar/baz/asdf/quux.html', '.html')); // 输出: 'quux'
path.dirname(path)
const path = require('path');
console.log(path.dirname('/foo/bar/baz/asdf/quux.html')); // 输出: '/foo/bar/baz/asdf'
path.extname(path)
const path = require('path');
console.log(path.extname('/foo/bar/baz/asdf/quux.html')); // 输出: '.html'
path.join([...paths])
const path = require('path');
console.log(path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')); // 输出: '/foo/bar/baz/asdf'
path.resolve([...paths])
const path = require('path');
console.log(path.resolve('/foo/bar', './baz')); // 输出: '/foo/bar/baz'
console.log(path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')); // 输出: '/tmp/subfile'
path.isAbsolute(path)
const path = require('path');
console.log(path.isAbsolute('/foo/bar')); // 输出: true
console.log(path.isAbsolute('quux/')); // 输出: false
These methods provide powerful functions for manipulating file paths to facilitate the management of files and directories. path
module, you first need to userequire('path')
Bring it in.