A way to reference / path to application directory (both for css and html)

12 views
Skip to first unread message

francesco....@gmail.com

unread,
May 12, 2013, 9:03:08 PM5/12/13
to node-...@googlegroups.com
I have an "issue" and would like to find a valid solution, If I use the / path (I'm on windows) I get files referenced from C:/ paths, which is not ok (I'm in like C:/Users/Francesco/Desktop...). Are there any solution to this?

Chris Turnbull

unread,
May 13, 2013, 8:48:29 AM5/13/13
to node-...@googlegroups.com, francesco....@gmail.com
I'm not sure if I've fully understood your problem here - can't you just use a relative path? E.g. if you're using windows, the relative path seems to be set to wherever nw.exe is placed.

Just out of curiosity, does anyone know the answer to the reverse of the problem? If someone is running your node-webkit application from the Applications/ or Program Files/ directory (which would be good practice) you will not be allowed to create or modify any files inside that directory without admin rights (which would be bad practice). How would you get the path to their User Directory so you could load/save files?

francesco....@gmail.com

unread,
May 13, 2013, 8:57:08 AM5/13/13
to node-...@googlegroups.com, francesco....@gmail.com
Having a relative path is not an option really, if you have to insert an <img> when you are under some complex paths, you'll end up with ../../../../../ which starts to be boring

francesco....@gmail.com

unread,
May 13, 2013, 9:10:56 AM5/13/13
to node-...@googlegroups.com, francesco....@gmail.com
I forget to specify what I mean, sorry:

Suppose my application is C:\Somwhere\bla\my_app.exe which has a C:\Somwhere\bla\index.html inside, I would like to be able to use / in my paths to refer to C:\Somwhere\bla or at least, the path relative to where index.html is. I mean, because I know node extracts the application in %appdata% (somewhere there), it's ok if / refers to that path, the important thing is that it references the "main" directory of my application, the same where package.json is maybe or something similar

app
  -- assets
  -- bla
  -- index.html

In this case the directory should be "app"
Can't it be done with some nodejs functionalities or similar?

Il giorno lunedì 13 maggio 2013 14:48:29 UTC+2, Chris Turnbull ha scritto:

Chris Turnbull

unread,
May 13, 2013, 10:19:22 AM5/13/13
to node-...@googlegroups.com
If you can work out how to change the working directory that Node Webkit uses, you could set it to the path of your index.html? You still wouldn't be able to use / in your paths, but you could use ./ or no prefix at all.


--
You received this message because you are subscribed to a topic in the Google Groups "node-webkit" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-webkit/odnXQAALSwg/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to node-webkit...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Chris Price

unread,
May 13, 2013, 11:49:33 AM5/13/13
to node-...@googlegroups.com
To get the user folder, you can use process.env:

userPath : function(){
var result = '';
switch(os.platform()){
case 'win32' :
result = process.env['userprofile'];
break;

case 'darwin' :
result = process.env['HOME'];
break;
}
return result;
}

From that, you can then either manually construct the path to %appdata% (though I just use process.env again on Windows [not sure if XP returns the correct folder or not, but I think it does]) or "Library/Application Support":

appData: function(){
var result = '';
switch(os.platform()){
case 'win32' :
result = process.env['appdata'];
break;

case 'darwin' :
result = path.join(this.userPath(), 'Library', 'Application Support');
break;
}
return result;
}

Francesco Belladonna

unread,
May 13, 2013, 11:52:27 AM5/13/13
to node-...@googlegroups.com
./ or no prefix overridden can be a problem (for images in same path of the page), basically I just need a / which "emulates" the "domain" path, like http://www.bla.bla/lol/asd the / path is http://www.bla.bla/


2013/5/13 Chris Turnbull <cjt...@gmail.com>

Chris Turnbull

unread,
May 13, 2013, 12:00:49 PM5/13/13
to node-...@googlegroups.com
@Chris: Thanks, that's going to be really helpful!

@Francesco: Just use ../../../../../ then? Surely it's correct answer and you have no reason not to.

Chris Price

unread,
May 13, 2013, 12:04:34 PM5/13/13
to node-...@googlegroups.com
From my reply... this probably makes more sense:

appData: function(){
var result = '';
switch(os.platform()){
case 'win32' :
result = process.env['appdata'];
break;

case 'darwin' :
result = path.join(process.env['HOME'], 'Library', 'Application Support');
break;
}
return result;
}

just so it doesn't have to run through the earlier switch again since you're already testing for platform. Also not sure of memory implications... I'm still wrapping my head around structure for JavaScript applications.

Francesco Belladonna

unread,
May 13, 2013, 12:06:21 PM5/13/13
to node-...@googlegroups.com
No because the path for the image will be different based on every page path (insane for not small APPS) and because I can't share front end code between a web app and a "desktop app" (node webkit app) without actually reworking my code in some way.

I accept it as an answer but I think something could be done about it


2013/5/13 Chris Turnbull <cjt...@gmail.com>
@Chris: Thanks, that's going to be really helpful!

@Francesco: Just use ../../../../../ then? Surely it's correct answer and you have no reason not to.

--

Chris Turnbull

unread,
May 13, 2013, 12:37:17 PM5/13/13
to node-...@googlegroups.com
Just an FYI, starting all your links with / in your web app might cause problems if you ever needed to move your code into a subdirectory.

Francesco Belladonna

unread,
May 13, 2013, 1:38:15 PM5/13/13
to node-...@googlegroups.com
That's definitely not true, I can just make a subdomain for that path and the issue won't exist anymore.
At the same time, if I move a directory inside a different subdirectory, instead of just appending the new path, I have to calculate the new ../../ based on the new path.


2013/5/13 Chris Turnbull <cjt...@gmail.com>
Just an FYI, starting all your links with / in your web app might cause problems if you ever needed to move your code into a subdirectory.

--
Reply all
Reply to author
Forward
0 new messages