iOS 使用node js 搭建简单的本地服务器
一.前提:基于iOS 项目 调用,使用了第三方框架NodeMobile。技术说明关键是 应用生命整个周期只能在应用启动时候开辟的一个线程里申请 一个 node js 资源。如果终止了运行,重启是不支持的。
“Currently, only a single instance of the Node.js runtime can be started within an application. Restarting the engine after it has finished running is also not supported.”
二.目标:能够跑起node js 本地服务,读取本地一段 json.
来实现 让客户端模拟服务请求,方便在 服务端 和客户端等 多端 同步开发,减少阻塞依赖。
三.集成node js 到项目步骤
(1)pod 'NodeMobile', :git => 'https://github.com/janeasystems/nodejs-mobile.git' 引入SDK (或者参考demo github 手动引入也可)
(2)此时运行程序会报错,提示该第三方库 不支持bitcode设置,需要在 BuildSetting 里 bitcode 布尔设置项改为NO ,项目能正常跑起。
(3)按照demo 模拟”本地js” 一路畅通
(4)模拟 node js 会闪退, 原因 a.不支持npm 命令 b.没有引入main.js 中 引用的节点 node js”left - pad”
(5) 安装npm 参考2
a.这里使用 场景是 在mac本上操作 iOS framework Node js
b.需要支持brew命令,因为我之前安装过,这个过程省略了
具体为参考3
c.使用brew命令下载 CMake : brew install cmake
(6)
1) Clone this repo and check out the mobile-master branch:
git clone https://github.com/janeasystems/nodejs-mobile cd nodejs-mobile git checkout mobile-master
2) Run the helper script: 配置node js 在Xcode 项目中使用
./tools/ios_framework_prepare.sh
(7)在项目中,蓝色文件作为资源使用,创建文件路径要选择create folder 才行. 防止 [[NSBundle mainBundle] pathForResource:@"nodejs-project/main.js" ofType:@""]; 找不到资源
(8)在项目 nodejs-project 内 执行命令 npm install
(9)添加 main.js 中引用的lef pad 节点 还是在(8)文件夹内执行 命令 npm install left-pad
至此,基本整个项目就跑通了。。。
四.客户端代码部分:
#import "AppDelegate.h"
#import "NodeRunner.h" @interface AppDelegate () @end @implementation AppDelegate - (void)startNode1 {
NSArray* nodeArguments = [NSArray arrayWithObjects:
@"node",
@"-e",
@"var http = require('http'); "
" var versions_server = http.createServer( (request, response) => { "
" response.end('Versions: ' + JSON.stringify(process.versions)); "
" }); "
" versions_server.listen(3000); "
,
nil
];
[NodeRunner startEngineWithArguments:nodeArguments];
} - (void)startNode {
NSString* srcPath = [[NSBundle mainBundle] pathForResource:@"nodejs-project/main.js" ofType:@""];//这个路径 是蓝色文件夹才行
NSArray* nodeArguments = [NSArray arrayWithObjects:
@"node",
srcPath,
nil
];
[NodeRunner startEngineWithArguments:nodeArguments];
} - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Currently, only a single instance of the Node.js runtime can be started within an application.
//Restarting the engine after it has finished running is also not supported.
NSThread* nodejsThread = nil;
nodejsThread = [[NSThread alloc]
initWithTarget:self
selector:@selector(startNode)
object:nil
];
// Set 2MB of stack space for the Node.js thread.
//The iOS node runtime expects to have 1MB of stack space available. Having 2MB of stack space available is recommended.
[nodejsThread setStackSize:2*1024*1024];
[nodejsThread start];
return YES;
}
#import "ViewController.h"
- (void)viewDidLoad {
[super viewDidLoad]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, self.view.frame.size.width, 88);
btn.backgroundColor = [UIColor yellowColor];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(myButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } - (void)myButtonAction:(id)sender
{
NSString *localNodeServerURL = @"http:/127.0.0.1:3000";//Localhost 代表的是本机的位置, 通常其对应的IP 是127.0.0.1 端口号3000根据需要自定义不被占用闲置的就好
NSURL *url = [NSURL URLWithString:localNodeServerURL];
NSString *versionsData = [NSString stringWithContentsOfURL:url];
if (versionsData) {
NSLog(@"%@",versionsData);//这里会输出目标结果 eg json
}
}
蓝色文件夹创建文件路径要选择create folder 才行.(nodejs-project 文件夹)
nodejs-project文件夹里面main.js 内容
var http = require('http'); //http 模块
var leftPad = require('left-pad');//left pad 模块 //
var fs = require('fs'); //文件模块
var path = require('path'); //系统路径模块 var jsonFile = './package.json';
var file = path.join(__dirname, jsonFile) //文件路径,__dirname为当前运行js文件的目录
//读取本地指定的一个json 文件并s回执
var versions_server = http.createServer((request, response) => {//开启一个本地服务
console.log('xx' + request);
fs.readFile(file,'utf8',function(err,data){
response.end(data); //这里输出读取路径jsonFile的json文件
}) }); /* var versions_server = http.createServer( (request, response) => { response.end('Versions: ' + JSON.stringify(process.versions) + ' left-pad: ' + leftPad(42, 5, '0')); }); */
versions_server.listen(3000); //监听预定使用的端口号3000
参考
1.https://code.janeasystems.com/nodejs-mobile/getting-started-ios
2.https://github.com/janeasystems/nodejs-mobile
4.https://blog.csdn.net/lihefei_coder/article/details/81453716
iOS 使用node js 搭建简单的本地服务器的更多相关文章
- node.js搭建简单的websocket
1.首先在官网http://www.nodejs.org/下载NODE.JS 2.打开命令行CMD,进入NODEJS\node_modules\的目录,输入npm install socket.io ...
- 用node.js实现简单的web服务器
node.js实现web服务器还是比较简单的,我了解node.js是从<node入门>开始的,如果你不了解node.js也可以看看! 我根据那书一步一步的练习完了,也的确大概了解了node ...
- node搭建简单的本地服务器
首先要安装node,方法很多,可以去网上找找,可以直接去官网下载安装,新版本的node是自带npm的: 安装好以后,新建一个js文件,名为server.js: let http = require(' ...
- 使用Node.js搭建静态资源服务器
对于Node.js新手,搭建一个静态资源服务器是个不错的锻炼,从最简单的返回文件或错误开始,渐进增强,还可以逐步加深对http的理解.那就开始吧,让我们的双手沾满网络请求! Note: 当然在项目中如 ...
- Node.js配合node-http-proxy解决本地开发ajax跨域问题
情景: 前后端分离,本地前端开发调用接口会有跨域问题,一般有以下3种解决方法: 1. 后端接口打包到本地运行(缺点:每次后端更新都要去测试服下一个更新包,还要在本地搭建java运行环境,麻烦) 2. ...
- Nodejs入门-基于Node.js的简单应用
服务端JavaScript 众所周知的,JavaScript是运行在浏览器的脚本语言,JavaScript通常作为客户端程序设计语言使用,以JavaScript写出的程序常在用户的浏览器上运行.直至N ...
- 使用 Node.js 搭建 Web 服务器
使用Node.js搭建Web服务器是学习Node.js比较全面的入门教程,因为实现Web服务器需要用到几个比较重要的模块:http模块.文件系统.url解析模块.路径解析模块.以及301重定向技术等, ...
- 使用 Node.js 搭建微服务网关
目录 Node.js 是什么 安装 node.js Node.js 入门 Node.js 应用场景 npm 镜像 使用 Node.js 搭建微服务网关 什么是微服务架构 使用 Node.js 实现反向 ...
- Docker最全教程之使用Node.js搭建团队技术文档站(二十三)
前言 各种编程语言均有其优势和生态,有兴趣的朋友完全可以涉猎多门语言.在平常的工作之中,也可以尝试选择相对适合的编程语言来完成相关的工作. 在团队技术文档站搭建这块,笔者尝试了许多框架,最终还是选择了 ...
随机推荐
- 列表框清屏/CListBox清空
CListBox自带方法: MyListBox->ResetContent(); CListBox用法: 关联一个变量m_List,m_List.AddString("test&quo ...
- SecureCRT 常用技巧
转自:http://blog.chinaunix.net/uid-26575352-id-3063143.html 快捷键: 1. ctrl + a : 移动光标到行首 2. ctrl + e :移 ...
- 【BZOJ2067】[Poi2004]SZN 二分+树上贪心
[BZOJ2067][Poi2004]SZN Description String-Toys joint-stock 公司需要你帮他们解决一个问题. 他们想制造一个没有环的连通图模型. 每个图都是由一 ...
- Bootstrap初学基础总结
Bootstrap 1>.Web UI 框架 可以帮助菜鸟程序员 ,迅速简便的搭建起专业级界面效果 2>如何快速掌握利用框架 1.框架的整合和搭建,让框架能够正常跑起来 2.通过复制粘贴文 ...
- SignalR 循序渐进(五)多个Hub服务器下的消息订阅
SignalR的通讯方式决定了其高性能,但是即便如此,当消息的并发量上来以后,单节点的Hub服务器依然可能无法承载总的消息吞吐量,那么如何对Hub服务器做水平扩展呢? 从微软官方的文档上看,Signa ...
- java递归构建菜单树
package testSimple; import java.util.ArrayList; import java.util.List; public class BuildTree { publ ...
- centos7修改hostname和hosts
1.修改/etc/hostname vi /etc/hostname 打开之后的内容是: localhost.localdomain 把它修改成想要的名字就可以,比如:master 保存退出 2.修改 ...
- oracle入门(6)——PL/SQL常用语法
[本文介绍] 本文不是”语法大全“,只是记录下作项目里自己常用的一些语法.方便查询. [语法] [输出] (1)输出语法 DBMS_OUTPUT.PUT_LINE( ) [定义] (1)定义变 ...
- (3.14) set statistics io/time/profile /SET SHOWPLAN_ALL ON详解统计信息与执行计划
SQL Server读懂语句运行的统计信息 SET STATISTICS TIME IO PROFILE ON 执行计划详细描述请参考(读懂执行计划) 对于语句的运行,除了执行计划本身,还有一些其他 ...
- 本书版权输出到台湾地区,《深入理解Android内核设计思想》诚挚感谢大家一直以来的支持!