简体   繁体   English

什么是path.resolve('。')输出?

[英]what is the path.resolve('.') output?

what will be the path if resolve('.') in node.js? 如果在node.js中resolve('。')将是什么路径?

I went through the documentation of path npm. 我浏览了路径npm的文档。 All I understood is resolve form the absolute path from right to left so it may include the current working directory for making absolute path and also remove the unwanted backslash or add if its need. 我所了解的是从右到左的绝对路径解析的,因此它可以包括用于生成绝对路径的当前工作目录,还可以删除不需要的反斜杠,或者在需要时添加反斜杠。

But nowhere it mention, what is the output for given below code? 但是没有提及,下面的代码输出是什么?

In additional to that, i need to know "Can I passing the variable which holds path as string and if i can then what will happen if I pass the number variable? 除此之外,我还需要知道“我是否可以将保存路径的变量传递为字符串,如果可以,那么如果我传递数字变量会怎样呢?

var Path = require('path');
console.log(Path.resolve('.'));

The output for that line will be the root of your project. 该行的输出将是项目的根。 I tried some different cases of running a node server from different directories. 我尝试了从不同目录运行节点服务器的一些不同情况。

my test.js is in C:\\Users\\Name\\Desktop\\Test\\test.js 我的test.js位于C:\\Users\\Name\\Desktop\\Test\\test.js

if I run the server from Test directory the output will be C:\\Users\\Name\\Desktop\\Test\\ 如果我从Test目录运行服务器,则输出将为C:\\Users\\Name\\Desktop\\Test\\

but if I go back one directory and run again the output will be C:\\Users\\Name\\Desktop\\ 但是如果我返回一个目录并再次运行,输出将为C:\\Users\\Name\\Desktop\\

Now if we move to a real scenario in production with a hosted server that line will give you the complete path where your server is hosted. 现在,如果我们转到生产中带有托管服务器的真实场景,那么该行将为您提供托管服务器的完整路径。 Let's say you have a showImage(url) function that takes an absolute path as a parameter and use it to show an image. 假设您有一个showImage(url)函数,该函数将绝对路径作为参数并使用它来显示图像。 You can get that url using Path variable like: 您可以使用Path变量获取该网址,例如:

showImage(Path.resolve('.') + "\images\image.jpg");

Keep in mind this is just an example you can use it to refer to any file in your project. 请记住,这只是一个示例,您可以使用它来引用项目中的任何文件。

For your second question this happens if you pass a path or multiple paths: 对于第二个问题,如果您通过一个或多个路径,则会发生这种情况:

 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 the current working directory is /home/myself/node, // this returns '/home/myself/node/wwwroot/static_files/gif/image.gif' 

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

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