|
2 | 2 |
|
3 | 3 | An Android Studio project that uses the [`Node.js on Mobile`]( https://github.com/janeasystems/nodejs-mobile) shared library, as an example of using a Node Project folder inside the Application.
|
4 | 4 |
|
5 |
| -The sample app runs the node.js engine in a background thread to start an HTTP server on port 3000. The app's Main Activity UI has a button to query the server and show the server's response (i.e. the `process.versions` value). Alternatively, it's also possible to access the server from a browser running on a different device connected to the same local network. |
| 5 | +The sample app runs the node.js engine in a background thread to start an HTTP server on port 3000. The app's Main Activity UI has a button to query the server and show the server's response (i.e. the `process.versions` value, alongside the result of using the [`left-pad` npm module](https://www.npmjs.com/package/left-pad)). Alternatively, it's also possible to access the server from a browser running on a different device connected to the same local network. |
6 | 6 |
|
7 | 7 | ## How to run
|
8 | 8 | - Clone this project.
|
9 |
| - - Download the Node.js on Mobile shared library from [here](https://github.com/janeasystems/nodejs-mobile/releases/download/nodejs-mobile-v0.1.4/nodejs-mobile-v0.1.4-android.zip). |
| 9 | + - Run `npm install` inside `android/native-gradle-node-folder/app/src/main/assets/nodejs-project/`. |
| 10 | + - Download the Node.js on Mobile shared library from [here](https://github.com/janeasystems/nodejs-mobile/releases/download/nodejs-mobile-v0.1.6/nodejs-mobile-v0.1.6-android.zip). |
10 | 11 | - Copy the `bin/` folder from inside the downloaded zip file to `app/libnode/bin` (There are `copy-libnode.so-here` files in each architecture's path for convenience). If it's been done correctly you'll end with the following paths for the binaries:
|
11 | 12 | - `app/libnode/bin/arm64-v8a/libnode.so`
|
12 | 13 | - `app/libnode/bin/armeabi-v7a/libnode.so`
|
@@ -46,7 +47,22 @@ console.log('The node project has started.');
|
46 | 47 | }
|
47 | 48 | ```
|
48 | 49 |
|
49 |
| -> Having a `nodejs-project` path with a `package.json` inside is helpful for using npm modules, by running `npm install {module_name}` inside `nodejs-project` so that the modules are also packaged with the application and made available at runtime. |
| 50 | +### Add an npm module to the `nodejs-project` |
| 51 | + |
| 52 | +Having a `nodejs-project` path with a `package.json` inside is helpful for using npm modules, by running `npm install {module_name}` inside `nodejs-project` so that the modules are also packaged with the application and made available at runtime. |
| 53 | + |
| 54 | +Install the `left-pad` module, by running `npm install left-pad` inside the `app/src/main/assets/nodejs-project/` folder. |
| 55 | + |
| 56 | +Update `app/src/main/assets/nodejs-project/main.js` to use the module: |
| 57 | +```js |
| 58 | +var http = require('http'); |
| 59 | +var leftPad = require('left-pad'); |
| 60 | +var versions_server = http.createServer( (request, response) => { |
| 61 | + response.end('Versions: ' + JSON.stringify(process.versions) + ' left-pad: ' + leftPad(42, 5, '0')); |
| 62 | +}); |
| 63 | +versions_server.listen(3000); |
| 64 | +console.log('The node project has started.'); |
| 65 | +``` |
50 | 66 |
|
51 | 67 | ### Copy the `nodejs-project` at runtime and start from there
|
52 | 68 |
|
|
0 commit comments