path.isAbsolute(path)


path.isAbsolute() 方法用于判断字面量 path 是否为绝对路径。因此,它不能安全地用于防止路径遍历。

【The path.isAbsolute() method determines if the literal path is absolute. Therefore, it’s not safe for mitigating path traversals.】

如果给定的 path 是一个零长度的字符串,将返回 false

【If the given path is a zero-length string, false will be returned.】

例如,在 POSIX 上:

【For example, on POSIX:】

path.isAbsolute('/foo/bar');   // true
path.isAbsolute('/baz/..');    // true
path.isAbsolute('/baz/../..'); // true
path.isAbsolute('qux/');       // false
path.isAbsolute('.');          // false 

在 Windows 上:

【On Windows:】

path.isAbsolute('//server');    // true
path.isAbsolute('\\\\server');  // true
path.isAbsolute('C:/foo/..');   // true
path.isAbsolute('C:\\foo\\..'); // true
path.isAbsolute('bar\\baz');    // false
path.isAbsolute('bar/baz');     // false
path.isAbsolute('.');           // false 

如果 path 不是字符串,则会抛出 TypeError

【A TypeError is thrown if path is not a string.】