NodeJs Electron 搭建基础桌面应用

Electron 概述

              Electron为用纯JavaScript创建桌面应用提供了运行时。原理是,Electron调用你在package.json中定义的main文件并执行它。main文件(通常被命名为main.js)会创建一个内含渲染完的web页面的应用窗口,并添加与你操作系统的原生GUI(图形界面)交互的功能。

第一步:基于NodeJs 环境,创建NodeJs 应用

C:\node_workspace>mkdir mongodb_desk

C:\node_workspace>cd mongodb_desk

C:\node_workspace\mongodb_desk>cnpm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (mongodb_desk)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to C:\node_workspace\mongodb_desk\package.json:

{
  "name": "mongodb_desk",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes) yes

C:\node_workspace\mongodb_desk>mkdir public

C:\node_workspace\mongodb_desk>mkdir routes

C:\node_workspace\mongodb_desk>mkdir views

第二步:本地安装Electron模块,本地项目依赖Electron模块。

C:\node_workspace\mongodb_desk>cnpm install electron
√ Installed 1 packages
√ Linked 132 latest versions
Downloading tmp-10380-1-SHASUMS256.txt-4.1.2
[============================================>] 100.0% of 4.74 kB (4.74 kB/s)
√ Run 1 scripts
Recently updated (since 2019-03-22): 3 packages (detail see file C:\node_workspace\mongodb_desk\node_modules\.recently_updates.txt)
  Today:
    → electron@*(4.1.2) (06:29:53)
√ All packages installed (142 packages installed from npm registry, used 15s(network 2s), speed 377.99kB/s, json 133(254.73kB), tarball 507.68kB)

编辑mongodb_desk应用的package.json,引入electron模块。

{
  "name": "mongodb_desk",
  "version": "1.0.0",
  "description": "",
  "main": "app.js", 
  "scripts": {
	"start": "electron ."
  },
  "author": "",
  "license": "ISC"
}

"main" 属性:指定nodejs 程序入口js 文件

"scripts"属性:指定本应用依赖的第三方模块(electron)

 

第三步:创建app.js 文件和index.html文件

项目(mongodb_desk)根路径创建app.js

const {
    app,
    BrowserWindow
} = require('electron')
const path = require('path')
const url = require('url')

function createWindow() {
    //创建浏览器窗口
    win = new BrowserWindow({
        width: 800,
        height: 600
    })

    //让浏览器加载index.html
    win.loadURL(url.format({
        pathname: path.join(__dirname, './views/index.html'),
        protocol: 'file:',
        slashes: true
    }))
}

//执行
app.on('ready', createWindow)

项目(mongodb_desk)/views 文件夹创建index.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
</head>

<body>
    <h1>Hello Electron!</h1>
</body>

</html>

第四步:nodejs 安装依赖和启动

C:\node_workspace\mongodb_desk>cnpm install
√ Installed 0 packages
√ Linked 0 latest versions
√ Run 0 scripts
√ All packages installed (used 33ms(network 15ms), speed 0B/s, json 0(0B), tarball 0B)

C:\node_workspace\mongodb_desk>cnpm start

> mongodb_desk@1.0.0 start C:\node_workspace\mongodb_desk
> electron .

效果截图:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值