简体   繁体   English

NodeJS-相对文件路径

[英]NodeJS - Relative File Paths

Say have a filepath var dir = "my/file/directory" and I supply a relative file path var rel = "../../other/directory/myFile.js" , how does one compute the real directory: 假设有一个文件路径var dir = "my/file/directory" ,我提供了一个相对文件路径var rel = "../../other/directory/myFile.js" ,如何计算真实目录:

var dir = "my/file/directory"
var rel = "../../other/directory/myFile.js"
var nDir = func(dir,rel) // returns "my/other/directory/myFile.js"

So far I have found path = require('path') and have been messing around with functions in REPL. 到目前为止,我发现path = require('path')并一直在使用REPL中的函数。 However I cannot see where the relative() function, is useful in determining the final path...? 但是我看不到relative()函数在确定最终路径时有用...?

As far as I can see: 据我所知:

var p1 = "root"
var p2 = "my/dir/file.js"
var O  = path.relative(p1,p2) //returns "../my/dir/file.js"

it is returning the relative path... where as, to me, this should return "root/my/dir/file.js" ? 它正在返回相对路径...在我看来,这应该返回"root/my/dir/file.js"

You can take a look at Node Docs on path.resolve , which returns an absolute path. 您可以在path.resolve上查看Node Docs ,它返回绝对路径。 path.relative will return a relative path. path.relative将返回相对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM