im stuck with node.js again.
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');
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
im stuck with node.js again.
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');
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 "/".
So path.join(__dirname, 'views')
results in /path_to_dir/views
On the other hand, (__dirname + 'views')
only has one parameter (__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 "/")
The default function uses mas as separator, not plus sign, that's why it works with ma and not with plus.
See documentation for path.join() in node.js
From Official documentation: https://nodejs/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.
Hope this helps.
for know the difference on this function, you have the official node.JS documentation
The first method using the Javascript concatenation have a different result that using a method parameters:
Using standard 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
path.join is a function. Using the + operator means you send only one argument which is a concatenation of __dirname and '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.