gpt4 book ai didi

node.js - 调试 Electron/Node 应用程序时如何显示网络面板

转载 作者:搜寻专家 更新时间:2023-10-31 23:03:13 25 4
gpt4 key购买 nike

我正在构建一个 Electron 应用程序,我正在使用库 (request/axios) 来发出请求。我没想到的一件事是,在 Chrome Debug模式下运行时,在 Node 上发出这些请求不会显示 Network Panel。有没有一种简单的方法告诉 Debug模式打开网络面板以调整到 https 请求(我假设这些库都使用 https)?


目前在我的服务器端 Electron 应用程序上我只看到以下内容 enter image description here

最佳答案

解决方案 1 - 创建您自己的

您可以 package 您的 axios 函数并将事件发送到您的渲染器进程

主要 Electron 过程

const electron = require('electron');

const {
app,
BrowserWindow,
ipcMain
} = electron;

const _axios = require('request-promise');

const axios = {
get: (url, params) => _axios.get(url, params).then(sendData),
post: (url, params) => _axios.post(url, params).then(sendData),
delete: (url, params) => _axios.delete(url, params).then(sendData),
put: (url, params) => _axios.put(url, params).then(sendData)
// ...
};

function sendData() {
return (data) => {
mainWindow.webContents.send('network', data);
return data;
};
}

渲染器进程(index.html):

<!DOCTYPE html>
<html>

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

<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css"

rel="stylesheet">
<style>
.kb-debug-widget {
position: fixed;
bottom: 0;
height: 200px;
overflow-x: hidden;
overflow-y: auto;
background: grey;
left: 0;
right: 0;
font-size: 10px;
}
</style>
</head>

<body>
<div class="kb-debug-widget">
<table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth"
id="network">
<tr>
<th>Name</th>
<th>Method</th>
<th>Status</th>
<th>Type</th>
<th>Body</th>
</tr>
</table>
</div>
<script>
require('./renderer.js');
var {
ipcRenderer,
remote
} = require('electron');

ipcRenderer.on('network', (event, response) => {
const networkElement = document.getElementById('network');

// print whatever you want here!
networkElement.innerHTML +=
`
<tr>
<td>${response.request.href}</td>
<td>${response.request.method}</td>
<td>${response.statusCode}</td>
<td>${response.headers['content-type']}</td>
<td>${response. data}</td>
</tr>
`;

// you can also print the network requests to the console with a decent UI by using console.table:
console.table({
name: response.request.href,
method: response.request.method,
status: response.statusCode,
type: response.headers['content-type'],
body: response. data,
});
});
</script>
</body>

</html>

这将在您的 View 底部创建一个小部件。

request 更简单:

const _request = require('request-promise');
const _axios = require('request-promise');

// this should cover all sub-methods
const request = (params, callback) => {
return _request(params, callback)
.on('response', (response) => {
mainWindow.webContents.send('network', response);
return response;
});
};

由于 axiosrequest 都返回相似的对象,因此您可以在渲染器端使用相同的函数。

实际代码

enter image description here

Here's a GitHub repository with the code implemented

解决方案 1:Alt - 将网络请求写入渲染器的控制台

我还添加了一个选项,使用 console.table 将请求打印到开发工具控制台。这是它的样子: enter image description here如果您不想在 HTML 中包含小部件,则可以仅保留此方法。

解决方案 2 - 使用 --inspect 标志运行 electron

你也可以只run electron with the inspect flag ,它允许您调试服务器代码,并有自己的网络选项卡,其中包含“服务器端”HTTP 请求。

为了看到它,像这样运行你的 Electron 应用程序:

electron --inspect=<port> your/app

如果您想在第一行立即中断,请运行相同的命令,但将 --inspect 替换为 --inspect-brk

运行命令后,打开任何网络浏览器并转到 chrome://inspect 并选择检查那里出现的已启动的 Electron 应用程序。 enter image description here

关于node.js - 调试 Electron/Node 应用程序时如何显示网络面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50090977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com