Hot File

Create Run Node.js with Express on Mobile Devices

View: 401    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: iPhone OS   Fields: Other

9 point/3 review File has been tested

We released a JXcore plugin for Apache Cordova recently and in this article I will show how to run a Nodeexpress application with Cordova.

Introduction

We released a JXcore plugin for Apache Cordova recently and in this article I will show how to run a Nodeexpress application with Cordova.

At the time of writing the jxcore-cordova project on github has two samples prepared for running the express module.

List of Samples

The project contains an install_and_run script (documented here), which simplifies creating a Cordova application and running 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 is installed on your system. If they are not, please refer to individual documentation on how to do this.

Plug an android device into a USB socket (with USB Debugging enabled), unless you want to run the application on the Android 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, you should see the following screen:

Android Launch Screen

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

http://10.0.2.15:3000
Running in Browser

We can see that the browser was 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 have noticed on the screen above, I did not use the IP and port mentioned before, but http://localhost:8080 instead. This is because I was running the sample on an AVD (Android Virtual Device), and the IP is not reachable outside the emulator’s internal router (see Emulator Networking for more details). Thus my solution was to establish a simple port redirection:

telnet localhost 5558
redir add tcp:8080:3000

Which redirects all http requests from my localhost:8080 into the emulator’s 3000 port. The 5558 number is the port on which my AVD was 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 is currently commented out, run those commands:

# or run on ios
$ cordova platforms add ios
$ cordova run ios
Running in iOS

This time accessing the Express server from the browser is more straightforward, for example,http://192.168.1.11:3000.

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 way as a regular Node.js application:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World! (' + Date.now() + ")");
});

var server = app.listen(3000, 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, but there is one major difference. It runs the express server in a separate thread unblocking the main thread. This is 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 (req, res) {
    res.send('Hello World! (' + Date.now() + ")");
  });

  var server = app.listen(3000, function () {
    clog("Express server is started. (port: 3000)");
  });
});

 

Create Run Node.js with Express on Mobile Devices

Create Run Node.js with Express on Mobile Devices Posted on 03-03-2016  We released a JXcore plugin for Apache Cordova recently and in this article I will show how to run a Nodeexpress application with Cordova. 3/10 401

Comment:

To comment you must be logged in members.

Files with category

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd