NodeJS中のPathモジュール

2511 ワード

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' ]

  
直接運転