2016-08-18 29 views
2

我已經能夠通過管理系統如如何在電子內運行表達?

https://github.com/theallmightyjohnmanning/electron-express

https://github.com/frankhale/electron-with-express

成功運行電子應用中的表現。然而,有人建議我不要這樣做由於GNU通用公共許可證,他們強加。我正在嘗試創建一個可以貨幣化的商業應用。因此,像麻省理工學院這樣的liscene可能會做,但不知道GNU。

不管怎樣,我一直在努力追隨他的過程: https://gist.github.com/maximilian-ruppert/a446a7ee87838a62099d

但在運行了一些問題。 這就是我迄今爲止所做的。

# Clone the Quick Start repository 
$ git clone https://github.com/electron/electron-quick-start 

# Go into the repository 
$ cd electron-quick-start 

# Install the dependencies and run 
$ npm install && npm start 

然後得到表達

$ express myapp 
$ cd myapp 

$ npm install 
renamed myapp to just app 

,現在我被困在那裏我需要配置電子main.js文件或部分/和渲染index.html文件鏈接到的快遞應用程序,並有運行而不是index.html

任何幫助,將不勝感激。

我在Windows 10

回答

2

包裝的Express應用程序在電子運行

首先,在您的應用程序

npm install --save electron 

創建包含index.html文件安裝的電子快遞申請

我們需要在我們的快速應用程序中加載的頂級文件。如果您沒有使用像Webpack這樣的模塊打包程序,那麼只需將您的應用程序依賴的所有html,cs和js文件導入到此index.html文件中。

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="UTF-8"> 
    <title>QuickMap</title> 
    <link href='public/css/boostrap.min.css' rel='stylesheet'> 
    <link href='public/css/layout.css' rel='stylesheet'> 
    </head> 
    <body> 
    <div id='root' /> 
    <script src='public/js/appBundle.js' type='text/javascript'></script> 
    <script src='public/js/bootstrap.min.js' type='text/javascript'></script> 
    <script src='public/js/jquery-3.1.1.min.js' type='text/javascript'></script> 
    </body> 
</html> 

確保您需要爲您的應用程序這個index.html的文件進口一切運行-i.e所有必要的HTML,CSS,JS和其他文件。請記住包含您的應用程序需要的任何外部文件,例如我們在上面的示例中加載的jQuery。

順便 - 包裝使用的WebPack

在這個例子中我們整個快速應用程序是由通過的index.html加載的WebPack束表示的電子應用程序。

請記住,您不需要使用Webpack將Express應用程序與Electron打包在一起。只要確保index.html加載你需要的所有文件,以啓動你的快速應用程序。

如果你使用的是Webpack,你的包應該被導入到這個index.html文件中。

這裏是導入包含我們明確的應用程序的捆綁的WebPack的例子index.html文件。

現在在項目中創建的根加載包含您的快速應用

這裏的index.html是電子將利用推出自己的主文件的電子配置文件。鋁電子相關的配置和鏈接到我們的明確程序(通過導入的WebPack束)包含在這裏。

注意下面的文件是屬於我們的根項目目錄,並從電子快速入門指南與線除外主要由樣板解釋高於進口,它加載整個應用我們的index.html文件。

main.js

const {app, BrowserWindow} = require('electron') 
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 win 

function createWindow() { 
    // Create the browser window. 
    win = new BrowserWindow({width: 800, height: 600}) 

    // and load the index.html of the app. 
    win.loadURL(url.format({ 
    pathname: path.join(__dirname, 'index.html'), 
    protocol: 'file:', 
    slashes: true 
    })) 

    // Open the DevTools. 
    win.webContents.openDevTools() 

    // Emitted when the window is closed. 
    win.on('closed',() => { 
    // 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. 
    win = 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',() => { 
    // On macOS 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',() => { 
    // On macOS 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 (win === 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. 

以下行加載我們的index.html我們之前創建一個指向我們的電子實例,以我們的應用程序的入口點。

mainWindow.loadURL(`file://${__dirname}/index.html`) 

改變你的package.json啓動腳本啓動電子

"scripts": { 
    "start": "ENV=development electron .", 
    }, 

現在,當我們運行

npm start 

電子會自動尋找並運行main.js文件在我們項目的根。

相關問題