Step by step _ node. js

Source: Internet
Author: User
Tags nets
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.