http服务
前端(渲染进程)与主进程通信 - http
# 发送http请求到主进程
内置http server服务
特点:可在前端(渲染进程)、浏览器、终端命令(curl)等,跨界访问主进程的方法。
// frontend/src/views/framework/socket/HttpServer.vue
import { ipcApiRoute, requestHttp } from '@/api/main'
<script>
sendRequest () {
// 打开【我的图片】
requestHttp(ipcApiRoute.doHttpRequest, {id: 'pictures'}).then(res => {
//console.log('res:', res)
})
},
</script>
// electron/controller/framework.js
const { app: electronApp, shell } = require('electron');
/**
* 一个http请求访问此方法
*/
async doHttpRequest() {
// http方法
const method = this.app.request.method;
// http get 参数
let params = this.app.request.query;
params = (params instanceof Object) ? params : JSON.parse(JSON.stringify(params));
// http post 参数
const body = this.app.request.body;
const httpInfo = {
method,
params,
body
}
if (!body.id) {
return false;
}
const dir = electronApp.getPath(body.id);
shell.openPath(dir);
return true;
}
// Make sure to add code blocks to your code group
# 完整代码
上次更新: 2025/04/10, 03:07:49