1 Star 0 Fork 0

free pan / electron-easy-mock

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

基于Electron实现的本地mock服务

作用与特点

用于mock后端接口,不用连接任何外部网,无需安装任何其他依赖,双击exe文件即可开始使用!

为什么不选 ApiFox ?

ApiFox需要登录,而登录就需要外网,而本人所处环境,外网访问受限

支持的功能

  • 普通REST API接口模拟
  • 流文件下载接口的模拟
  • 接口延迟模拟
  • 接口异常模拟

作者

coder_pan

打赏/捐助

打赏/捐赠

源码仓库

https://gitee.com/free_pan/electron-easy-mock

必须的依赖

  • @doubleshot/nest-electron
  • @nestjs/microservices

源码本地运行说明

  • pnpm >= 9.x
  • node >= 20.x
  • 本地已安装 python,且配置好了环境变量(sqlite3需要python, 进行编译)
  • 网络环境通畅(运行electron项目,没有通畅的网络环境,那要做很多额外的工作, 且费力不讨好)

exe运行说明

  • 首次启动较慢, 因为要执行自动创建表等初始化工作, 再次启动速度就正常了

主要使用技术

  • vite
  • electron
  • nestjs
  • typeorm
  • better-sqlite3
  • vue3
  • ant design vue

electron事件通信方式

preload 向浏览器注入通信桥对象

src/preload/index.js

import { contextBridge, ipcRenderer } from 'electron'
// 通过contextBridge.exposeInMainWorld向浏览器中注册对象,js通过调用该对象中的方法间接向主进程发送事件
contextBridge.exposeInMainWorld('electron', {
  restart:()=>{
    // 向主进程发送事件
    ipcRenderer.invoke('restart')
  },
  sendMsg: (msg: string): Promise<string> => ipcRenderer.invoke('msg', msg),
  onReplyMsg: (cb: (msg: string) => any) =>
    ipcRenderer.on('reply-msg', (e, msg: string) => {
      cb(msg)
    }),

})

页面(js)中直接调用桥对象中的方法

// window.electron 是自行注册的通信桥对象
const { restart } = window.electron
// 调用通信桥对象中的方法,由桥方法向electron主进程发送事件
restart()

electron 主进程中 new BrowserWindow() 之后,注册监听方法,监听桥对象发送的事件,并执行相应的代码逻辑

import { app, ipcMain } from 'electron'

const win = new BrowserWindow({
  width: 1024,
  height: 768,
  autoHideMenuBar: true,
  webPreferences: {
    contextIsolation: true,
    preload: join(__dirname, '../preload/index.js'),
  },
})

ipcMain.handle('restart', () => {
  // 调用relaunch方法不会真正重启(但必须调用,相当于告诉exit方法,我这次不是直接退出,而是要重启)
  app.relaunch()
  // 调用exit,才会退出然后重启(如果不调用relaunch方法,直接就是exit,那么就只是退出,而不是重启)
  app.exit()
})

参考资源

electron 相关问题

electron builder打包时,出现errorOut=ERROR: Cannot create symbolic link

electron sqlite集成相关问题

Electron 中集成 SQLite3 数据库的最佳实践

vite electron nestjs 整合问题

vite + electron + nestjs整合模板

doubleshot: 用于整合 vite 与 electron的插件

electron better-sqlite3 整合问题

windows下,electron安装better-sqlite3并编译

在 Electron上安装better-sqlite3出错

Electron 中安装Better-sqlite3遇到的问题总结

nestjs better-sqlite3 knex.js 整合问题

nest-knexjs整合

Knex与better-sqlite3整合

electron-log

By default, it writes logs to the following locations:

on Linux: ~/.config/{app name}/logs/main.log on macOS: ~/Library/Logs/{app name}/main.log on Windows: %USERPROFILE%\AppData\Roaming\{app name}\logs\main.log

主进程中使用

import log from 'electron-log/main'

// Optional, initialize the logger for any renderer process
log.initialize()

log.info('Log from the main process')

渲染进程中使用

import log from 'electron-log/renderer'
log.info('Log from the renderer process')

打包注意事项

只会打包dependencies中的依赖,所以非必要以来请放到devDependencies

MIT License Copyright (c) 2020-present ArcherGu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

后端接口mock工具(适用于无法访问外网的情况) 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/free_pan/electron-easy-mock.git
git@gitee.com:free_pan/electron-easy-mock.git
free_pan
electron-easy-mock
electron-easy-mock
master

搜索帮助