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.
How to Run Node.js with Express on Mobile Devices的更多相关文章
- Code Your First API With Node.js and Express: Set Up the Server
How to Set Up an Express API Server in Node.js In the previous tutorial, we learned what the REST ar ...
- Node.js、express、mongodb 实现分页查询、条件搜索
前言 在上一篇Node.js.express.mongodb 入门(基于easyui datagrid增删改查) 的基础上实现了分页查询.带条件搜索. 实现效果 1.列表第一页. 2.列表第二页 3. ...
- Node.js、express、mongodb 入门(基于easyui datagrid增删改查)
前言 从在本机(win8.1)环境安装相关环境到做完这个demo大概不到两周时间,刚开始只是在本机安装环境并没有敲个Demo,从周末开始断断续续的想写一个,按照惯性思维就写一个增删改查吧,一方面是体验 ...
- node.js和express.js安装和使用步骤 [windows]
PS: NODEJS:https://nodejs.org NPM:https://www.npmjs.com/ 一.node.js安装与配置 到https://nodejs.org/en/downl ...
- Node.js基于Express框架搭建一个简单的注册登录Web功能
这个小应用使用到了node.js bootstrap express 以及数据库的操作 :使用mongoose对象模型来操作 mongodb 如果没了解过的可以先去基本了解一下相关概念~ 首先注 ...
- node.js框架express的安装
node.js框架express的安装 首先假定你已经安装了 Node.js,接下来为你的应用创建一个目录,然后进入此目录并将其作为当前工作目录. $ mkdir myapp $ cd myapp 通 ...
- Node.js系列-express(上)
前言 Node.js系列的第一篇:http,大概描述了通过使用node.js内置的api创建一个服务并监听request实现简单的增删改查.现在,我们就通过通读express官网及使用express框 ...
- node.js,express入门看详细篇
先最简单的代码 安装 npm install express app.js 代码内容 const express = require('express') const app = express() ...
- node.js使用express框架进行文件上传
关于node.js使用express框架进行文件上传,主要来自于最近对Settings-Sync插件做的研究.目前的研究算是取得的比较好的进展.Settings-Sync中通过快捷键上传文件,其实主要 ...
随机推荐
- SQL*PLUS中批量执行SQL语句
SQL*PLUS中批量执行SQL语句 今天由于工作的需要,要在CMD中批量执行大量的SQL语句,对于Oracle学习还处在入门阶段的我,只能硬着头皮到处去寻找资料(主要是网络资料,也包括自己的电子书) ...
- php的标记形式
共三种: 推荐第一种,第三种需要在php.ini中配置 效果: 第三种配置 将short_open_tag=Off改为On重启Apache就可以了
- vi删除多行,替换,复制
VI中的多行删除与复制 法一: 单行删除,:1(待删除行)d 多行删除 ,:1,10d 法二: 光标所在行,dd 光标所在行以下的N行,Ndd 方法1: 光标放到第6行, 输入:2yy 光标放到第9行 ...
- linux运维常用命令
1.linux启动过程 开启电源 --> BIOS开机自检 --> 引导程序lilo或grub--> 内核的引导(kernel boot)--> 执行init(rc.sysin ...
- c#调用c++ dll(二)
当对c++几种调用方式有了解以后我们可以试着写个c++动态连接库了,我们现在来写个简单的c++求和函数并把它封装成dll,供以后的c#调用 我们写dll的时候,个人认为,要写就要把dll写好,写标准, ...
- HashMap使用
/* * 测试HashMap的应用,判断 */ import java.util.HashMap; public class HuaWeiTest { private static final Int ...
- MySQL备份方案
下面将分别模拟不同场景数据库宕机解决方案:这里应用到的技术分别为innobackuper及binlog日志来进入还原数据 一.主从库情况下(为了不影响主库的性能,备份都放在从库上进行)当主库宕机时,如 ...
- 第三篇、C_双向链表(循环链表)
简介: 在用C/C++开发系统中,我们知道用数组或者单链表来开发,如果是数据比较大的话,性能很不好,效率也不高.因此常常需要考虑系统的实用性,常常采用双向链表来开发. 示例: 1.数据 typedef ...
- iOS开发,多个button数组,每个数组只能选中5项,多个数组只能选择3个。
由于常用xib,所以不想用代码写那么多个button.而且也懒的算位置 直接xib拉线成四个数组.水果,零食,饮料,甜点. 入题实现的功能就是,在这四个数组之中只能在3个数组只选中5项.有点绕(就比如 ...
- Android--LowMemoryKiller知识点补充
Android在内存管理上与linux有些小的区别.其中一个就是引入了Low memory killer . 1.引入原因: Android是一个多任务系统,也就是说可以同时运行多个程序,这个大家应该 ...