var path = require('path'); // 當發現有多個連續的斜槓時,會替換成一個; 當路徑末尾包含斜槓時,會保留; // 在 Windows 系統會使用反斜槓。 var p = path.normalize('foo/bar//baz/asdf/quux/..') ; var p2 = path.normalize('foo/bar//baz/asdf/quux/../') ; console.log('p',p,p2); //組合參數中的全部路徑,返回規範化後的路徑。 var p3 = path.join('a/b/c',"d/c/v"); console.log('p3',p3); // 返回文件的絕對路徑 var p4 = path.resolve('test.js'); var p5 = path.resolve('/path/test.js'); console.log('p4',p4); console.log('p5',p5); // var ab = path.isAbsolute('test.js'); // console.log('ab',ab); // 返回從 from 到 to 的相對路徑 var res = path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb'); console.log('res',res); //path.resolve(from, path.relative(from, to)) == path.resolve(to) // 文件的文件夾名稱 var dir1 = path.dirname('../test.js'); var dir2 = path.dirname('./test.js'); var dir3 = path.dirname('test.js'); var dir4 = path.dirname('/foo/bar/baz/asdf/quux'); //dir1 .. //dir2 . //dir3 . //dir4 /foo/bar/baz/asdf console.log('dir1',dir1); console.log('dir2',dir2); console.log('dir3',dir3); console.log('dir4',dir4); // 返回後文件基本名稱 第二個參數爲 後綴名若是存在則返回除去後綴之後的名字 var base1 = path.basename('/foo/bar/baz/asdf/quux.html', '.html'); var base1 = path.basename('/foo/bar/baz/asdf/quux.html', '.htm'); var base2 = path.basename('/foo/bar/baz/asdf/quux.html', 'html'); var base3 = path.basename('/foo/bar/baz/asdf/quux.html'); // base1 quux // base2 quux. // base3 quux.html console.log('base1',base1); console.log('base2',base2); console.log('base3',base3); // 返回後綴名, 沒有則爲空 var en1 = path.extname('test.js'); var en2 = path.extname('test'); console.log('en1 ', en1); console.log('en2 ', en2); //en1 .js //en2 // path.sep 路徑分隔符 var sp = 'foo/bar/baz'.split(path.sep); //sp [ 'foo', 'bar', 'baz' ] console.log('sp', sp); // path.delimiter多個路徑之間的分隔符 var del = process.env.PATH .split(path.delimiter); console.log('del', del); //de [ '/usr/local/bin', // '/usr/bin', // '/bin', // '/usr/sbin', // '/sbin', // '/usr/local/bin', // '/usr/local/git/bin' ]
直接運行javascript