learn:path.mjs 917 B

12345678910111213141516171819202122232425
  1. import { resolve, join, extname, basename, dirname } from 'node:path';
  2. export function testJoin() {
  3. console.log(join('/foo', 'bar', 'baz/asdf', 'quux', '..'));
  4. }
  5. export function testResolve() {
  6. console.log(resolve('/foo/bar', './baz'));
  7. console.log(resolve('/foo/bar', '/tmp/file/'));
  8. console.log(resolve('wwwroot', 'static_files/png/', '../gif/image.gif'));
  9. // /users/daxia/documents/work/vip20/19_Node.js/day2/code/wwwroot/statci_files/git/image.gif
  10. // /Users/daxia/Documents/work/vip20/19_Node.js/day-2/code/wwwroot/static_files/gif/image.gif
  11. }
  12. // 其他
  13. export function otherMethods() {
  14. const filePath = './a/b/c/d.md';
  15. // 获取文件的扩展名
  16. console.log('后缀名:', extname(filePath));
  17. // 获取文件的完整名称(包含 后缀名)
  18. console.log('文件名:', basename(filePath));
  19. // 文件的目录层级
  20. console.log('文件的目录层级:', dirname(filePath));
  21. }