React Native (简称RN)是Facebook开源的跨平台移动应用开发框架,使用Javascript语言,着力于提高多平台开发的开发效率 —— 仅需学习一次,编写任何平台。(Learn once, write anywhere)
Node.js是一个Javascript运行环境(runtime)。实际上它是对Google V8引擎进行了封装。是一个基于Chrome JavaScript运行时建立的平台, 用于方便地搭建响应速度快、易于扩展的网络应用。
一、React-native
React-native本就是为了跨平台而生,自然可以完美打造出一套移动端开发流程,兼容android、ios。语法简单,用起来不难。这里推荐一下中文版官网,有能力的童鞋可以看英文版的官网,更新会更快一些。
二、Nodejs
开发服务端是不是大家秒想到的是不是Javaweb?PHP?这里给大家推荐一种相当便捷的开发模式:Nodejs
1
2
3
4
5
6
7
8var fs = require('fs'); var express = require('express'); var app = express(); app.get('/a', function (req, res) { res.send('success'); }); app.listen(8880);
使用了最简便的express框架,通过以上Demo便可以使用http://localhost:8880/a 访问到该请求了,当然,不要忘记加上自定义的命名空间。返回值即success。
三、React-Web + Nodejs
参看了下淘宝团队打造出的React-Web,即可将移动端平移至Web端,除一些特殊复杂的业务,兼容性可观。
1
2
3
4
5
6
7
8
9
10var fs = require('fs'); var express = require('express'); var app = express(); // app.use(express.static(__dirname)); app.get('/a', function (req, res) { res.sendFile('/Users/dawn/react-native-web- example/web/output/index.html'); }); app.listen(8800);
自己学习使用了React-Web,也写过一个Demo放到Github上:React-Web,主要的步骤大致是:
- npm install
- npm start
- react-web start
- react-web bundle(optional)
效果如下:(熟悉React-native的童鞋应该都看出来了,几本上完美兼容~)

四、React-Web + Electron
既然强大的React-native + Nodejs可以打通前后端流程,那么开发PC端也不在话下~这里介绍Electron框架的实现方式。核心代码main.js如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52'use strict' const electron = require('electron') // Module to control application life. const app = require('app') // Module to create native browser window. const BrowserWindow = require('browser-window'); const path = require('path') const url = require('url') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let mainWindow = null function createWindow () { // Create the browser window. mainWindow = new BrowserWindow({width: 702, height: 600}) // and load the index.html of the app. mainWindow.loadUrl(url.format({ pathname: path.join(__dirname, 'index.html'), protocol: 'file:', slashes: true })) // Open the DevTools. // mainWindow.webContents.openDevTools() // Emitted when the window is closed. mainWindow.on('closed', function () { // Dereference the window object, usually you would store windows // in an array if your app supports multi windows, this is the time // when you should delete the corresponding element. mainWindow = null }) } // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.on('ready', createWindow) // Quit when all windows are closed. app.on('window-all-closed', function () { // On OS X it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform !== 'darwin') { app.quit() } }) app.on('activate', function () { // On OS X it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (mainWindow === null) { createWindow() } }) // In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and require them here.
需要注意的是,配置文件需要自定义一下,这样便可使用简单的npm run-script package命令一键打包,亲测可用。
1
2
3
4
5"scripts": { "start": "electron .", "package": "electron-packager /Users/dawn/electron-quick-start Ivor --platform=darwin --electronVersion=0.30.2 --out=output --overwrite --icon=/Users/dawn/electron-quick-start/app-icon.icns" }
由于这部分是使用了国外网址,npm install timeout的童鞋可以把根目录的.npmrc文件添加上如下内容(使用淘宝镜像源):
1
2
3
4
5registry=https://registry.npm.taobao.org sass_binary_site=https://npm.taobao.org/mirrors/node-sass/ phantomjs_cdnurl=http://npm.taobao.org/mirrors/phantomjs ELECTRON_MIRROR=http://npm.taobao.org/mirrors/electron/
然后刚开始使用的时候,直接引用官方提供的Demo,会遇到各种坑,主要体现在package.json的版本与自定义操作方面,于是自己也努力打通了完整流程,可以参看一下我的Electron_Demo。
完成了上上方核心代码main.js的功能后,对应app( )的的各个方法,包含了对Window的操作,一个完整的桌面端应用就出来啦,撒花~

</br>
-
-
-
-
- 如有问题,欢迎随时评论交流!
-
-
-
-
-
-
-
- 未完待续,后期会补充完善细节,敬请期待!
-
-
-
最后
以上就是苹果火最近收集整理的关于React-native + Nodejs打造移动端 + 服务端 + PC端 + Web端全套服务的全部内容,更多相关React-native内容请搜索靠谱客的其他文章。
发表评论 取消回复