Electron子进程开启Koa微服务

栏目: IT技术 · 发布时间: 4年前

内容简介:会将程序退出时,需要终止子进程

Electron子进程开启Koa微服务

1、配置 Server 打包位置

// vue.config.js
pluginOptions: {
    electronBuilder: {
      builderOptions: {
        //...
        // 匹配server中的所有文件夹和文件
        extraResources: ['server/**/**'] // 指定打包 Server 到 Resource 文件夹中。
      }
    }
}

会将 server 文件夹打包到 Resources 文件夹中。为什么不打包大 app.asar 中呢?因为 .asar 文件是只读的,无法进行数据库的写入。

2、配置webpack

// vue.config.js
configureWebpack: {
  externals: ['pg', 'sqlite3', 'tedious', 'pg-hstore']
}

3、创建 Electron 子进程

function createServerProcess() {
  if (!isDevelopment) {
    // 生产环境
    serverProcess = fork('../server/index.js', [], {
      cwd: path.join(__dirname, '../server')
    })
  } else {
    // 开发环境
    serverProcess = fork(require.resolve('../server/index.js'))
    serverProcess.on('close', code => {
      console.log('子线程已经退出', code)
    })
  }
}

程序退出时,需要终止子进程 process.kill(serverProcess.pid)

至此:Mac可以启动服务和访问数据库

问题

windows 下无法启动服务 && Please install sqlite3 错误

  1. 在 Sequelize 中指定数据库的类型,默认有时无法读取到。

  2. 执行 cnpm i 命令安装依赖包,会自动对 sqlite 3进行编译, node_module/sqlite3/lib/binding 中可以看到文件。或者执行命令 npm run postinstall (管理员身份执行, npm i 会默认执行该命令)

  3. 或者手动编译 npm rebuild 进行手动编译。

  4. 执行命令编译为 electron匹配的 sqlite3 文件。

node-gyp rebuild --target=6.1.0 --arch=x64 --target_platform=win32 --dist-url=https://npm.taobao.org/mirrors/node/ --module_name=node_sqlite3 --module_path=../lib/binding/electron-v6.1-win32-x64
  1. target 为 electron 版本,或者 node 版本
  2. –arch 架构;–traget_platform: 目标系统
  3. –dist-url: Header 文件下载地址

注:参考另一篇文中: Koa使用Sqlite3和Sequelize


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms Unlocked

Algorithms Unlocked

Thomas H. Cormen / The MIT Press / 2013-3-1 / USD 25.00

Have you ever wondered how your GPS can find the fastest way to your destination, selecting one route from seemingly countless possibilities in mere seconds? How your credit card account number is pro......一起来看看 《Algorithms Unlocked》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具