React-native + Nodejs打造移動端 + 服務(wù)端 + PC端 + Web端全套服務(wù)

React Native (簡稱RN)是Facebook開源的跨平臺移動應(yīng)用開發(fā)框架,使用Javascript語言,著力于提高多平臺開發(fā)的開發(fā)效率 —— 僅需學(xué)習(xí)一次,編寫任何平臺。(Learn once, write anywhere)

Node.js是一個Javascript運(yùn)行環(huán)境(runtime)。實際上它是對Google V8引擎進(jìn)行了封裝。是一個基于Chrome JavaScript運(yùn)行時建立的平臺, 用于方便地搭建響應(yīng)速度快、易于擴(kuò)展的網(wǎng)絡(luò)應(yīng)用。

一、React-native

React-native本就是為了跨平臺而生,自然可以完美打造出一套移動端開發(fā)流程,兼容android、ios。語法簡單,用起來不難。這里推薦一下中文版官網(wǎng),有能力的童鞋可以看英文版的官網(wǎng),更新會更快一些。

二、Nodejs

開發(fā)服務(wù)端是不是大家秒想到的是不是Javaweb?PHP?這里給大家推薦一種相當(dāng)便捷的開發(fā)模式: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 訪問到該請求了,當(dāng)然,不要忘記加上自定義的命名空間。返回值即success。

三、React-Web + Nodejs

參看了下淘寶團(tuán)隊打造出的React-Web,即可將移動端平移至Web端,除一些特殊復(fù)雜的業(yè)務(wù),兼容性可觀。

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

自己學(xué)習(xí)使用了React-Web,也寫過一個Demo放到Github上:React-Web,主要的步驟大致是:

  • npm install
  • npm start
  • react-web start
  • react-web bundle(optional)

效果如下:(熟悉React-native的童鞋應(yīng)該都看出來了,幾本上完美兼容~)

react-web-demo.png

四、React-Web + Electron

既然強(qiáng)大的React-native + Nodejs可以打通前后端流程,那么開發(fā)PC端也不在話下~這里介紹Electron框架的實現(xiàn)方式。核心代碼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"
}

由于這部分是使用了國外網(wǎng)址,npm install timeout的童鞋可以把根目錄的.npmrc文件添加上如下內(nèi)容(使用淘寶鏡像源):

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,會遇到各種坑,主要體現(xiàn)在package.json的版本與自定義操作方面,于是自己也努力打通了完整流程,可以參看一下我的Electron_Demo。

完成了上上方核心代碼main.js的功能后,對應(yīng)app( )的的各個方法,包含了對Window的操作,一個完整的桌面端應(yīng)用就出來啦,撒花~


electron_demo.png

</br>

          • 如有問題,歡迎隨時評論交流!
          • 未完待續(xù),后期會補(bǔ)充完善細(xì)節(jié),敬請期待!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容