パス確認
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
var path = 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' }