path.resolve和path.join

1、path.resolve(ag1,ag2,...)

path.resolve返回當前環境所在路徑拼接參數字符串所得到的絕對路徑,其中參數可以有多個

const path = require('path');

path.resolve('/foo/bar', './baz')
// returns '/foo/bar/baz'

path.resolve('/foo/bar', '/tmp/file/')
// returns '/tmp/file'

path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
// if the current working directory is /home/myself/node,
// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'

2、path.join(ag1,ag2,...)

const path=require('path');

path.join('a','b');
//返回 a\b

path.join(__dirname,'c','main.js')
//返回 D:\c\main.js  ,__dirname表示當前運行環境絕對路徑

//path.join()方法拼接路徑,並返回該路徑,結合__dirname可以達到path.resolve()方法同樣的效果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章