简体   繁体   English

在node.js中区别beetwen path.join(__ dirname,'views')和path.join(__ dirname +'views')

[英]difference beetwen path.join(__dirname , 'views') and path.join(__dirname + 'views') in node.js

im stuck with node.js again. 我再次陷入node.js。

i try the first path.join in my code for setup view engine its not working, the index won't showing up, then i change to path.join(__dirname , 'views'); 我尝试在我的代码中使用设置视图引擎的第一个path.join无法正常工作,索引不会显示,然后更改为path.join(__dirname , 'views'); and its working well, the index showing, i dont know how 及其运作良好,索引显示,我不知道如何

can someone explain how this work ? 有人可以解释这是如何工作的吗?

path.join(__dirname + 'views');

path.join(__dirname, 'views');

this is my script 这是我的剧本

app.set('views', path.join(__dirname, 'views')); //its work
app.set('views', path.join(__dirname + 'views')); // if i change like this, its not work

path.join constructs a path from your arguments by concatenating them with "/". path.join通过将参数与“ /”串联来从其参数构造路径。 So path.join(__dirname, 'views') results in /path_to_dir/views 所以path.join(__dirname, 'views')导致/ path_to_dir / views

On the other hand, (__dirname + 'views') only has one parameter (__dirname+views). 另一方面, (__dirname + 'views')仅具有一个参数(__dirname + views)。 This means there are no additional arguments to join to the first and so you end up with "/path_to_dirviews" (No joining "/") 这意味着没有其他参数可以加入第一个参数,因此您最终得到“ / path_to_dirviews”(不加入“ /”)

The default function uses commas as separator, not plus sign, that's why it works with comma and not with plus. 默认函数使用逗号作为分隔符,而不是加号,这就是为什么它使用逗号而不是加号的原因。

See documentation for path.join() in node.js 请参阅node.js中path.join()的文档

From Official documentation: https://nodejs.org/api/path.html#path_path_join_paths 摘自官方文档: https : //nodejs.org/api/path.html#path_path_join_paths

Paths that u join must be separated by a delimiter; 您加入的路径必须由定界符分隔;

Also if all paths are not strings, then an error will be thrown. 同样,如果所有路径都不是字符串,则将引发错误。

So, I presume the addition of + makes path go crazy because, it cannot find a delimiter and also it finds that the paths that u have mentioned are not all strings. 因此,我认为+的添加使path变得疯狂,因为它找不到分隔符,并且还发现您提到的路径不是全部字符串。

Hope this helps. 希望这可以帮助。

for know the difference on this function, you have the official node.JS documentation 要知道此功能的区别,您具有官方的node.JS文档

The first method using the Javascript concatenation have a different result that using a method parameters: 使用Javascript串联的第一种方法与使用方法参数的结果不同:

Using standard javascript : 使用标准javascript:

let folder = '/folder';
let subFolder = 'sub';

path.join(folder + sub ) // result : /foldersub

But, with properly use the paramters of this method, the path is different: This method format all parameters for create a standard part with / 但是,正确使用此方法的参数后,路径会有所不同:此方法使用/格式化所有参数以创建标准零件

let folder = '/folder';
let subFolder = 'sub';

path.join(folder, sub) // result : /folder/sub

Don't forget you cano use the console.log(path) for debug in Javascript... link for using it here 不要忘记,您可以使用console.log(path)进行Java调试... 在此处使用链接

path.join is a function. path.join是一个函数。 Using the + operator means you send only one argument which is a concatenation of __dirname and 'views'. 使用+运算符意味着您仅发送一个参数,该参数是__dirname和'views'的串联。 The function should return a path which is constructed from the arguments passed. 该函数应返回根据传递的参数构造的路径。 In you case just one, so it won't be a valid path. 在您的情况下,只有一个,因此它不是有效路径。

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

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