console.log(path.normalize('a/../../user/bin/../../../../'));//......
console.log(path.normalize('./a/.././user/bin/./'));//userbin
path.join([path1], [path2], […])
将多个路径结合在一起,并转换为规范化路径
var path = require('path');console.log(path.join('////./a', 'b////c', 'user/'));//abcuser
console.log(path.join('a', '../../', 'user/'));//..user
绝对和相对
path.resolve([from …], to)
从源地址 from 到目的地址 to 的绝对路径,类似在shell里执行一系列的cd命令
path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')类似于:
cd foo/barcd /tmp/file/
cd ..
cd a/../subfile
pwd
[注意]如果某个from或to参数是绝对路径(比如 ‘E:/abc’,或是以“/”开头的路径),则将忽略之前的from参数
var path = require('path');console.log(path.resolve('.', 'testFiles/..', 'trdLayer'));//D:projecttrdLayer
console.log(path.resolve('..', 'testFiles', 'a.txt'));//D:testFilesa.txt
console.log(path.resolve('D:', 'abc', 'D:/a'));//D:a
console.log(path.resolve('abc', 'ok.gif'));//D:projectabcok.gif
console.log(path.resolve('abc', '..', 'a/../subfile')); //D:projectsubfile
path.isAbsolute(path)
path是一个绝对路径(比如 ‘E:/abc’),或者是以“/”开头的路径,二者都会返回true
var path = require('path');console.log(path.isAbsolute('../testFiles/secLayer'));//false
console.log(path.isAbsolute('./join.js'));//false
console.log(path.isAbsolute('temp'));//false
console.log(path.isAbsolute('/temp/../..'));//true
console.log(path.isAbsolute('E:/github/nodeAPI/abc/efg'));//true
console.log(path.isAbsolute('///temp123'));//true
path.relative(from, to)
获取从 from 到 to 的相对路径,可以看作 path.resolve 的相反实现
path.resolve(from, path.relative(from, to)) == path.resolve(to)var path = require('path');
console.log(path.relative('C:test', 'C:implbbb'));//..implbbb
console.log(path.relative('C:/test/aaa', 'C:/bbb'));//....bbb
更多关于node.JS路径解析的方法请查看下面的相关链接









