React-native + Nodejs打造移动端 + 服务端 + PC端 + Web端全套服务

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

var 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端,除一些特殊复杂的业务,兼容性可观。

var 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的童鞋应该都看出来了,几本上完美兼容~)

1875573-def7ee5460e4c953.png
react-web-demo.png

四、React-Web + Electron

既然强大的React-native + Nodejs可以打通前后端流程,那么开发PC端也不在话下~这里介绍Electron框架的实现方式。核心代码main.js如下:

'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命令一键打包,亲测可用。

"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文件添加上如下内容(使用淘宝镜像源):

registry=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的操作,一个完整的桌面端应用就出来啦,撒花~


1875573-7b2a0b04e7c62c47.jpeg
electron_demo.png

</br>

          • 如有问题,欢迎随时评论交流!
          • 未完待续,后期会补充完善细节,敬请期待!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值