Skip to content

Commit 86d2165

Browse files
android: add npm module to node folder sample
Adds the use of a npm module to the native-gradle-node-folder sample. Also updates the nodejs-mobile binary version used.
1 parent bc43097 commit 86d2165

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

android/native-gradle-node-folder/README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
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.
44

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.
66

77
## How to run
88
- 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).
1011
- 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:
1112
- `app/libnode/bin/arm64-v8a/libnode.so`
1213
- `app/libnode/bin/armeabi-v7a/libnode.so`
@@ -46,7 +47,22 @@ console.log('The node project has started.');
4647
}
4748
```
4849

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+
```
5066

5167
### Copy the `nodejs-project` at runtime and start from there
5268

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var http = require('http');
2+
var leftPad = require('left-pad');
23
var versions_server = http.createServer( (request, response) => {
3-
response.end('Versions: ' + JSON.stringify(process.versions));
4+
response.end('Versions: ' + JSON.stringify(process.versions) + ' left-pad: ' + leftPad(42, 5, '0'));
45
});
56
versions_server.listen(3000);
67
console.log('The node project has started.');

android/native-gradle-node-folder/app/src/main/assets/nodejs-project/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
"description": "node part of the project",
55
"main": "main.js",
66
"author": "janeasystems",
7-
"license": ""
7+
"license": "",
8+
"dependencies": {
9+
"left-pad": "1.3.0"
10+
}
811
}

0 commit comments

Comments
 (0)