How to Run node. js with Express on Mobile Devices

Source: Internet
Author: User

We released a Jxcore plugin for Apache Cordova recently and the This article I'll show how to run a Node Express Applicat Ion with Cordova.

At the time of writing the Jxcore-cordova project on GitHub have the samples prepared for running the Express module.

The project contains an Install_and_run script (documented here), which simplifies creating a Cordova application and Runn ing the samples. I ' m going to use the script in this article.

Express on Android

The script assumes that Apache Cordova and the Android SDK are installed on your system. If They is not, please refer to individual documentation "on".

Plug an Android device into a USB socket (with USB debugging enabled), unless you want to run the application on the Andro ID Emulator.

Download the script and save it into an empty folder. Run it with a sample folder name as an argument, for example "Express sample":

./install_and_run.sh "express sample"

Shortly, should see the following screen:

The application displays the IP addresses the device is using and which port the express server was running on (i n our case). Take this URL and use it in your browser, i.e.:

http://10.0.2.15:3000

We can see that the browser is able to connect to our Express server running on the device and receive the proper answer For the request.

A Note for emulator Users:as you might has noticed on the screen above, I do not use the IP and port mentioned before, But http://localhost:8080 instead. This is because I am running the sample on a AVD (Android Virtual Device), and the IP is not reachable outside the Emula Tor ' s internal router (seeemulator Networking for more details). Thus My solution is to establish a simple port redirection:

5558redir add tcp:8080:3000

which redirects all HTTP requests from my localhost:8080 into the emulator ' s port. The 5558number is the port on which my avd were running (visible at AVD's title bar).

Express on IOS

We can run the same sample on IOS devices. The install_and_run.sh script can handle it, but the IOS support was currently commented out, run those commands:

# or run on ios$ cordova platforms add ios$ cordova run ios

This time accessing the Express server from the browser are more straightforward, for example,http://192.168.1.11:3000< /c5>.

Looking at the code

Looking at the app.js file located in the Www/jxcore folder of the Express sample, the Express Server is implemented in the same as a regular node. JS application:

var Express=Require(' Express ');Var app=Express(); app.Get(‘/‘,function(reqRes){Res.Send(' Hello world! (‘+ Date.Now()+")")}) ;var server = app.listen3000 function  ({clog " Express server is started. (port:3000) " ;}        
Express server running on another thread

Let's look at the other example:

./install_and_run.sh "express performance sample"

This example performs similarly and there is one major difference. It runs the express server in a separate thread unblocking the main thread. This was easy with Jxcore as it offers multitasking, before it even arrived on mobile platforms.

This is the code:

Jxcore. tasks.AddTask(function(){var clog=Require('./utilities '). log;var Express=Require(' Express ');Var app=Express(); App.Get(‘/‘,function(reqRes){Res.Send(' Hello world! (‘+ Date.Now()+")");})var server = app.listen (3000function  ( {clog ( "Express server is started. (port:3000) " }) ;}     

Note:the code is similar to the previous example. But was wrapped in a jxcore.tasks.addTask() invocation which handles the logic related to running the block in a separate instance.

Conclusion

The Express web Framework is one of the most popular and important modules in the node. JS ecosystem. With Jxcore it's possible to make it run on mobile devices and it brings a range of features (including MULTITHREADING/MU Ltitasking and packaging) that can is used in the Mobile world.

How to Run node. js with Express on Mobile Devices

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.