JaneaSystems / nodejs-mobile-samples

Repository for demo applications that use Node.js on Mobile
MIT License
168 stars 111 forks source link

Calling structured node scripts in folder #4

Open medopaw opened 6 years ago

medopaw commented 6 years ago

I'm trying to get a node script run with no luck. The script calls other scripts in a structured folder. Where should I put the folder? Should I add it to project or copy it into Document folder on app?

Hope you can provide a sample doing that. Thanks.

jaimecbernardo commented 6 years ago

Hi @medopaw . Could you please provide more information? Are you trying to run iOS or Android? Are you using the core library or a plugin? Is this issue related to a specific sample, since this is the samples repo? Thank you, in advance.

medopaw commented 6 years ago

@jaimecbernardo

Sorry for the incomplete information. I am following iOS native-xcode example, which uses libnode.framework. The sample runs well, and its calling node command part goes like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSArray* nodeArguments = [NSArray arrayWithObjects:
                                  @"node",
                                  @"-e",
                                  @"var http = require('http'); "
                                  " var versions_server = http.createServer( (request, response) => { "
                                  "   response.end('Versions: ' + JSON.stringify(process.versions)); "
                                  " }); "
                                  " versions_server.listen(3000); "
                                  ,
                                  nil
                                  ];
        [NodeRunner startEngineWithArguments:nodeArguments];
    });
    return YES;
}

I want to take a step further and run a nodejs script file instead of calling node -e <script string>. Then I am not sure where I should put the script file to call node <script file path>. That's the first thing.

The second thing is, in most cases the nodejs script file does not work alone. It comes in a folder and calls other scripts. I would greatly appreciate it if you can provide a sample doing that. Thank you in advance.

jaimecbernardo commented 6 years ago

Hi @medopaw ,

Thank you for the added information.

A sample has been added to start node with a nodejs-project folder in iOS: https://github.com/janeasystems/nodejs-mobile-samples/tree/master/ios/native-xcode-node-folder

I hope this is helpful.

medopaw commented 6 years ago

@jaimecbernardo

The folder sample works great! Thank you so much. This is exactly what I want. And I'm sure it will help others who try to run nodejs scripts in a folder.

luguodushi commented 6 years ago

@jaimecbernardo

I have a sample to know how to start node with a nodejs-project folder in Android(native-gradle)? Thanks.

jaimecbernardo commented 6 years ago

Hi @luguodushi ,

There were plans for this sample, though it is not as simple as in the iOS case.

The sample for Android has been added here: https://github.com/janeasystems/nodejs-mobile-samples/tree/master/android/native-gradle-node-folder

I hope this is helpful.

luguodushi commented 6 years ago

@jaimecbernardo

It works great and thank you.

Can I use some 3rd modules like sqlite3, socket.io in nodejs-project? When I tried to use sqlite3, I got an error.

03-22 13:42:08.335 5832-5874/com.yourorg.sample E/NODEJS-MOBILE: module.js:529
                                                                     throw err;
Error: Cannot find module '/data/data/com.yourorg.sample/files/nodejs-project/node_modules/sqlite3/lib/binding/node-v57-android-arm64/node_sqlite3.node'
      at Function.Module._resolveFilename (module.js:527:15)
      at Function.Module._load (module.js:476:23)
      at Module.require (module.js:568:17)
      at require (internal/module.js:11:18)
      at Object.<anonymous> (/data/data/com.yourorg.sample/files/nodejs-project/node_modules/sqlite3/lib/sqlite3.js:4:15)
      at Module._compile (module.js:624:30)
      at Object.Module._extensions..js (module.js:635:10)
      at Module.load (module.js:545:32)
      at tryModuleLoad (module.js:508:12)
      at Function.Module._load (module.js:500:3)
jaimecbernardo commented 6 years ago

Hi @luguodushi ,

Some modules contain native C++ code, which means they have to be compiled for the target OS / architecture. There is a experimental process to do it in the nodejs-mobile-cordova and nodejs-mobile-react-native plugins, but no automatic process for "pluginless" scenarios yet.

I've previously verified that sqlite3 works in Android when used with the plugins.

Here's the issue tracking native modules in case you want to follow it: https://github.com/janeasystems/nodejs-mobile/issues/4#issuecomment-371555631