パス確認
node.jsパスモジュールはディレクトリとファイルパスが取れる。先ずは今のパスを確認する。
app.js
console.log(__dirname);//ディレクトリパスconsole.log(__filename);//ファイルパス
terminal
$node app.js
/Users/xxxxx/node_practice/path
/Users/xxxxx/node_practice/path/app.js
パス重要な関数のまとめ
app.js
varpath=require('path');//今のディレクトリのパスconsole.log(path.dirname(__dirname));//ディレクトリを組み合わせconsole.log(path.join(__dirname,'/xx'));//ファイル名console.log(path.basename(__filename));//ファイル拡張子console.log(path.extname(__filename));//パス分析console.log(path.parse(__filename));
terminal
$node app.js
/Users/xxxxx/node_practice/path
/Users/xxxxx/node_practice/path/xx
app.js
.js
{ root: '/',
dir: '/Users/xxxxx/node_practice/path',
base: 'app.js',
ext: '.js',
name: 'app' }