How to Run Node.js with Express on Mobile Devices

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

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

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:

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

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 (seeEmulator 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 5558number 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

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)");
  });
});

Note: The code is similar to the previous example. But is 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 is possible to make it run on mobile devices and it brings a range of features (including multithreading/multitasking and packaging) that can be used in the mobile world.

时间: 2024-08-30 15:15:26

How to Run Node.js with Express on Mobile Devices的相关文章

node.js基于express框架搭建一个简单的注册登录Web功能

这个小应用使用到了node.js  bootstrap  express  以及数据库的操作 :使用mongoose对象模型来操作 mongodb 如果没了解过的可以先去基本了解一下相关概念~ 首先注明一下版本,因为express因为版本的不同使用的方式也不同,我这算是目前最新的了吧 还没有装express的可以移步到这里 看看express框架的获取安装 1.简单地项目初始化 进入你的nodejs安装路径下边,如图,然后执行命令  express -e test  (这里把项目名设置为test

node.js 安装express 提示 command is not found

在安装express时增加generator参数: npm install -g express-generator 为什么要加上generator呢? 原先的express带cli, 现在把cli拆成了单独的express-generator包. 原先的express运行生成的项目是node app.js, 因为httpserver相关代码都在app.js里, 现在这部分代码移到了项目目录的bin/www下面, app.js只保留实现app的逻辑代码, 你需要去运行那个bin/www. 只是很

node.js框架 express

express是在node.js的基础上,拓展出的一个简洁实用的框架结构,运用这个东西,我们可以更方便的处理很多的事情.只要上手了,那就是个贝多芬! 一般安装express有几种方法. 第一,使用npm安装,cmd中输入npm install express -g,这个-g是全局安装,也就是安装在被你用"config set global"设置的文件夹里,需要注意的是,安装完了以后,需要改变环境变量以及其路径来指向你的安装目录. 第二,复制粘贴.(--废话!)不过这样的存在安全性问题,

Node.js、express、mongodb 入门(基于easyui datagrid增删改查)

前言 从在本机(win8.1)环境安装相关环境到做完这个demo大概不到两周时间,刚开始只是在本机安装环境并没有敲个Demo,从周末开始断断续续的想写一个,按照惯性思维就写一个增删改查吧,一方面是体验下node.js的魔力,二就是看看node.js.express和mongoose的API,其次就是把自己入门的过程记录下来,方便自己查看,再就是对入门的朋友起一个引导的作用. 敲demo的过程中感觉最爽的就是npm(Node Package Manager)是一个Node.js的包管理和分发工具.

Installing Node.js and Express on Ubuntu

Installing Node.js and Express on Ubuntu 1. 在nodejs官网上下载Linux Binaries(已经包含了npm):2. 安装Node.js下载后解压,并在解压的文件夹中启动Terminal后,输入命令:    sudo cp * /usr/local/ -r再输入命令:    node -v    npm -v查看程序版本,来检测是否成功安装: 3. 安装express    npm install -g express-generator 4.

node.js框架express的安装

首先假定你已经安装了 Node.js,接下来为你的应用创建一个目录,然后进入此目录并将其作为当前工作目录. $ mkdir myapp $ cd myapp 通过 npm init 命令为你的应用创建一个 package.json 文件. 欲了解 package.json 是如何起作用的,请参考 Specifics of npm’s package.json handling. $ npm init 此命令将要求你输入几个参数,例如此应用的名称和版本. 你可以直接按“回车”键接受默认设置即可,下

Mac环境下装node.js,npm,express;(包括express command not found)

1. 下载node.js for Mac 地址: http://nodejs.org/download/ 直接下载 pkg的,双击安装,一路点next,很容易就搞定了. 安装完会提醒注意 node和npm的路径是 /usr/local/bin. 看到一些帖子,用Homebrew安装也很容易的,我偷懒没有试,这边MK下:http://freemem.diandian.com/post/2012-06-02/40028564785. 当前最新的node.js安装完成包括了npm的,测试下是否安装成功

[js高手之路]Node.js+jade+express+mongodb+mongoose+promise实现todolist

promise主要是用来解决异步回调问题,其实还有好几种比promise更好的方案,后面再说,这节,我们先用promise来改造下,我以前写的一篇文章[js高手之路]javascript腾讯面试题学习封装一个简易的异步队列 中的一道面试题( 页面上有一个按钮,一个ul,点击按钮的时候,每隔1秒钟向ul的后面追加一个li, 一共追加10个,li的内容从0开始技术( 0, 1, 2, ....9 ) ). promise的小实例: 1 function next1(){ 2 return new P

基于Node.js和express的日志服务器

首先,这篇文章学习的意义大于实际价值.如果按我的本意,直接在游戏中加入友盟,信息更全,而且非常简单.不过总是有很多人会凭着自己过时或者错误的经验去说别的东西多么不好,自己的东西多么好.好在,我自认为学习能力非常强,解决问题的能力也非常强.真让我做一个服务器+前端,也是在兴趣之中和能力之内. 一.Node.js简介 原本javascript纯粹是一个前端语言,干的基本上是让网页更丰富更炫的事情.不过Node.js出现后,javacript成为了前后端通吃的语言.比如网易的pomelo就是基于Nod