This article describes how to build a nodejs server on an iPhone. This article provides detailed operation steps and commands. For more information, see
1. compile jxcore for running on ios
The code is as follows:
$ Mkdir ~ /Jxcore
$ Cd ~ /Jxcore
$ Git clone https://github.com/jxcore/jxcore.git
The code is as follows:
$ Cd ~ /Jxcore
$./Build_scripts/ios-compile.sh
If the module not found of import which occurs, use the following statement to install which of python.
The code is as follows:
Sudo easy_install tools/which-1.1.0-py2.7.egg
If other problems occur, refer to the prerequisites for compiling ios with jxcore and take corresponding measures.
Https://github.com/jxcore/jxcore/blob/master/doc/HOW_TO_COMPILE.md
The code is as follows:
* GCC 4.2 or newer (for SpiderMonkey builds 4.7 +)
* Python 2.6 or 2.7
* GNU Make 3.81 or newer
* Libexecinfo (FreeBSD and OpenBSD only)
* For SpiderMonkey: 'Which 'python module (sudo easy_install tools/which-1.1.0-py2.7.egg)
2. install jxcore on mac
The code is as follows:
$./Configure
$ Sudo make install
3. create the cordova program. if cordova is not installed, install it on your own.
The code is as follows:
$ Cordova create hello com. example. hello HelloWorld
$ Cd hello
4. download and install the jxcore-cordova Plugin
The code is as follows:
$ Git clone https://github.com/jxcore/jxcore-cordova
Use the template index.html of jxcore-cordovato
The code is as follows:
$ Cp./jxcore-cordova/sample/www/index.html./www/
Copy the jxcore package compiled in step 1 for ios
The code is as follows:
$ Cp-r ~ /Jxcore/out_ios/ios/bin jxcore-cordova/io. jxcore. node/
5. add ios platform for cordova
The code is as follows:
$ Cordova platforms add ios
$ Cordova plugin add jxcore-cordova/io. jxcore. node/
$ Cordova build
$ Cordova run ios
If a build error occurs, "C does not support default arguments"
You only need to remove the default value to solve the problem.
Set
The code is as follows:
JXCORE_EXTERN (void)
JX_SetString (JXValue * value, const char * val, const int32_t length = 0 );
Change
The code is as follows:
JXCORE_EXTERN (void)
JX_SetString (JXValue * value, const char * val, const int32_t length );
Re-build
6. you can see the running interface of cordova.
7. add nodejs server in Resources/jxcore_app/app. js
Add the following code at the end of app. js:
The code is as follows:
Function getIP (){
Var OS = require ('OS ');
Var nets = OS. networkInterfaces ();
Console. log (nets );
For (var a in nets ){
Var ifaces = nets [a];
For (var o in ifaces ){
If (ifaces [o]. family = "IPv4 "&&! Ifaces [o]. internal) {return ifaces [o]. address ;}
}
}
Return null;
}
Var ip = getIP ();
If (! Ip ){
Console. error ("You shoshould connect to a network! ");
Return;
}
Var http = require ('http ');
Http. createServer (function (req, res ){
Res. writeHead (200 ,{
'Content-type': 'Text/plain'
});
Var cur_client = "";
If (req. connection & req. connection. remoteAddress ){
Console. log (req. connection. remoteAddress );
Cur_client = req. connection. remoteAddress;
} Else if (req. headers ){
Console. log ("request header X-Forwarded-");
Console. log (req. headers ['X-Forwarded-For ']);
Cur_client = req. headers ['X-Forwarded-For '];
}
Cordova ('log'). call ('client ('+ cur_client +') come ');
Res. end ('Hello' + cur_client + ', I am server on iphone app (' + ip + ').' + Date. now () + '\ n ');
}). Listen (1337, ip );
Console. log ('server running at http: // '+ ip +': 1337 /');
Run the program to view the iphone ip address in the xcode log information, and then browse the webpage through the webpage.