NodeJs path.resolve的使用
__dirname
- __dirname 指向运行代码的文件夹
console.info('__dirname', __dirname) // C:\Leslie\Web_learning\Daily-interview
resolve 1.不带参数时,返回当前文件的绝对路径
console.info(path.resolve()) // C:\Leslie\Web_learning\Daily-interview
resolve 2.带不是以/开头的参数,返回的是当前路径拼接现在的参数
console.info(path.resolve('foo')) // C:\Leslie\Web_learning\Daily-interview\foo
console.info(path.resolve('foo', 'baz')) // C:\Leslie\Web_learning\Daily-interview\foo\baz
resolve 3.带./的参数,返回的是当前路径拼接现在的参数
console.info(path.resolve('./foo')) // C:\Leslie\Web_learning\Daily-interview\foo
console.info(path.resolve('./foo', './baz')) // C:\Leslie\Web_learning\Daily-interview\foo\baz
resolve 4.带/的参数,返回的是 / + 最后一个前面加/参数的文件名 + 剩下的参数
console.info(path.resolve('/a', '/b', 'c')) // c:\a
console.info(path.resolve(__dirname)) // C:\Leslie\Web_learning\Daily-interview
console.info(path.resolve('foo')) // C:\Leslie\Web_learning\Daily-interview\foo
console.info(path.resolve('/foo')) // C:\foo
console.info(path.resolve('./foo')) // C:\Leslie\Web_learning\Daily-interview\foo
console.info(path.resolve('../foo')) // C:\Leslie\Web_learning\foo
🚀🚀🚀 join
- path.join()方法使用平台特定的分隔符把全部给定的 path 片段连接到一起,并规范化生成的路径
console.info(path.join(__dirname, '/foo')) // C:\Leslie\Web_learning\Daily-interview\foo
console.info(path.join(__dirname, 'foo')) // C:\Leslie\Web_learning\Daily-interview\foo
console.info(path.join('/a', '/b')) // \a\b
Keep learning