Skip to content

Commit bc43097

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

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

ios/native-xcode-node-folder/README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
An iOS Xcode 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 and return the `process.versions` value. The app's Main ViewController UI has a button to query the server and show the server's response. 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 and return the `process.versions` value alongside the result of using the [`left-pad` npm module](https://www.npmjs.com/package/left-pad). The app's Main ViewController UI has a button to query the server and show the server's response. 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
## Prerequisites
88
To run the sample on iOS you need:
@@ -12,7 +12,8 @@ To run the sample on iOS you need:
1212

1313
## How to run
1414
- Clone this project.
15-
- 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-ios.zip).
15+
- Run `npm install` inside `ios/native-xcode-node-folder/nodejs-project/`.
16+
- 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-ios.zip).
1617
- Copy the `NodeMobile.framework` file inside the zip's `Release-universal` path to this project's `NodeMobile/` folder (there's a `copy-NodeMobile.framework-here` empty file inside the project's folder for convenience).
1718
- In Xcode import the `ios/native-xcode-node-folder/native-xcode-node-folder.xcodeproj` project.
1819
- Select one physical iOS device as the run target.
@@ -49,7 +50,21 @@ versions_server.listen(3000);
4950
}
5051
```
5152

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+
### Add an npm module to the `nodejs-project`
54+
55+
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.
56+
57+
Install the `left-pad` module, by running `npm install left-pad` inside the `nodejs-project` folder.
58+
59+
Update `main.js` to use the module:
60+
```js
61+
var http = require('http');
62+
var leftPad = require('left-pad');
63+
var versions_server = http.createServer( (request, response) => {
64+
response.end('Versions: ' + JSON.stringify(process.versions) + ' left-pad: ' + leftPad(42, 5, '0'));
65+
});
66+
versions_server.listen(3000);
67+
```
5368

5469
### Start the node runtime from the Node Project
5570

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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);

ios/native-xcode-node-folder/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)