Work with the filesystem API

For developers using the Construct 2 Javascript SDK

Post » Sun May 11, 2014 6:40 pm

Hello, this is my first post :D

This is the thing. I want to implement my own directory reader. I've been checking the AJAX and Nodewebkit plugin and both use the "fs" variable. I'm guessing that "fs" stands for "filesystem", but I'm not sure about that. I can't find nothing about that in the manual and when I try to use something like those plugins do, it always fails with a "require is not defined" error message.

This is the code both plugins use :

Code: Select all
      if (isNodeWebkit)
      {
         path = require("path");
         fs = require("fs");
         nw_appfolder = path["dirname"](process["execPath"]) + "\\";
      }


But I don't know where is defined the 'require' function, neither the "fs" or "path". If I try with my plugin to print those values it always throws that error.

Is there anyway to work with the filesystem api, or do I have to create an instance of it by myself? Because I can't find nothing similar to that in the plugins.

Thanks in advance.
B
4
Posts: 4
Reputation: 208

Post » Mon May 12, 2014 7:03 pm

Yes, fs stands for filesystem, it is an implementation from nodejs's fs module, you can read more about it here:

http://nodejs.org/api/fs.html


the require function comes with node, read more about it here:

http://nodejs.org/api/modules.html


Are you previewing your project in nodewebkit? because fs (and require and node stuff) is not available inside the browser!
Be nice until it's time to not be nice
B
36
S
10
G
9
Posts: 293
Reputation: 6,712

Post » Wed May 14, 2014 2:03 pm

Actually I'm previewing in Chrome. My idea is trying to implement my own directory reader. I thought that the filesystem variable was a C2 thing, but now you let me know it's part of nodewebkit.

I'll see it later with a little more time.

Thanks for your answer, it really helped me out to understand how it works.
B
4
Posts: 4
Reputation: 208

Post » Wed May 14, 2014 2:30 pm

Another question...

According to what you said, if I'm not using Nodewebkit the filesystem api will not work (that's what I understood). In other words, if I try to create a HTML5 application able to read a directory (the project directory) I will have to use the nodewebkit? Is there a chance to just preview in nodewebkit and then add the node.js library to the application and run it in every browser?

Thanks for your answer in advance, again.


PS: I've been checking if I can give you reputation points, but I can't find the option. Perhaps is earned automatically by answering a post, anyway, you deserve it. Thanks.
B
4
Posts: 4
Reputation: 208

Post » Wed May 14, 2014 3:22 pm

Unfortunately, nope, you can't get the "real" fs (and any other node stuff) inside a normal chrome browser.

The magic of node-webkit is in the combination of node.js, and chromium browser (former webkit, currently blink), hence the node-webkit. check the git project hosted on github for more info on nodewebkit.

You could check out the awesome browserify-fs project (https://github.com/mafintosh/browserify-fs) it has the "fs" module implementation for chrome, but it does not give you access to the whole system, it just emulates "fs" inside leveldb so you cannot read some file on c:/documents/whatever , it just stores a file inside local storage. (but you can store a pdf using browserify-fs, or a .doc, or a .capx file, if you're into it)

I didnt use the browserify-fs project yet, so you're on your own there, but it might be useful, depending on your requirements.

edit: looks like browserify doesent support fs check out: www.browserify.org
Be nice until it's time to not be nice
B
36
S
10
G
9
Posts: 293
Reputation: 6,712

Post » Wed May 14, 2014 6:27 pm

Oh, well, that's good to know. I'll check everything you said.

Thanks again for your help.

Have a nice day :)
B
4
Posts: 4
Reputation: 208


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 2 guests