如何检查路径是绝对路径还是相对路径
问题描述:
UNIX绝对路径以'/'开头,而Windows以字母'C:'或'\'开头. node.js是否具有标准的多平台功能来检查路径是绝对路径还是相对路径?
UNIX absolute path starts with '/', whereas Windows starts with alphabet 'C:' or '\'. Does node.js has a standard multiplatform function to check if a path is absolute or relative ?
答
自节点版本0.12.0起,您可以使用路径模块中的"noreferrer> path.isAbsolute(path)
函数.
Since node version 0.12.0 you can use the path.isAbsolute(path)
function from the path module.
即:
var path = require('path');
if(path.isAbsolute(myPath)) {
//...
}