首页 / 脚本样式 / JavaScript / node.js中的path.resolve方法使用说明
方法说明:
将参数 to 位置的字符解析到一个绝对路径里。
语法:
复制代码 代码如下:
path.resolve([from ...], to)
由于该方法属于path模块,使用前需要引入path模块(var path= require(“path”) )
接收参数:
from 源路径
to 将被解析到绝对路径的字符串
例子:
复制代码 代码如下:
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 currently in /home/myself/node, it returns
"/home/myself/node/wwwroot/static_files/gif/image.gif"
另一种方法是把它作为一个序列的cd命令shell。
复制代码 代码如下:
path.resolve("foo/bar", "/tmp/file/", "..", "a/../subfile")
类似于:
复制代码 代码如下:
cd foo/bar
cd /tmp/file/
cd ..
cd a/../subfile
pwd