Nodejs node-webkit-agent 网页端怎样用? // 晕, 是项目没完成
Nodejs node-webkit-agent 网页端怎样用? // 晕, 是项目没完成
听说有个 Node-agent 的调试工具, 用了 Chrome devtools 的 API Github 只搜到 node-webkit-agent , 就装起来尝试一下 先是为了省事全局安装了:
npm install -g webkit-devtools-agent
然后拷贝代码跑起来:
var agent = require('webkit-devtools-agent');
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, '127.0.0.1');
console.log('[%s] Server running at http://127.0.0.1:8080/', process.pid);
终端输出是这样的:
➤➤ node main.js
[22800] Server running at http://127.0.0.1:8080/
然后是发信号给 Chrome:
➤➤ kill -SIGUSR2 22800
之后输出的内容变成了下面这样, 貌似是个网页端地址:
➤➤ node main.js
[22800] Server running at http://127.0.0.1:8080/
webkit-devtools-agent: Spawning websocket service process...
webkit-devtools-agent: A proxy got connected.
webkit-devtools-agent: Waiting for commands...
webkit-devtools-agent: Websockets service started on 0.0.0.0:9999
然后我就进了 http://localhost:9999/
查看, 结果 HTML 返回内容是:
Not implemented
不知道这个东西应该怎样用, 求助…O.O
原来 9999 只是 webscoket 的监听端口, 要从网页端访问
http://c4milo.github.io/node-webkit-agent/26.0.1410.65/inspector.html?host=localhost:9999&page=0
页面进去是一个调试工具… 进去是 Safari 一样的让选择启动调试工具的界面
可是我点击 enable debugging
没有反应… 不知道, 没有任何反应…
Node v0.10.5 Chrome Version 28.0.1500.5 dev Archlinux
文档没详细看懂的问题… 原来对应的功能没有实现起来 目前只有 Profiles 的功能搜集 JS CPU 和 Memory 占用, Debugger 部分没有成功…
我还以为是用了 Android 一样的 remote debugger 工具用原生控制台 一个坑…
针对你提到的 node-webkit-agent
在网页端使用的疑问,我会提供一个详细的使用说明,并附上一些示例代码。首先需要明确的是,node-webkit-agent
并不是用于调试 Node.js 应用的标准工具,而更多地是为基于 Node.js 的前端开发提供类似于 Chrome DevTools 的调试体验。
使用步骤
-
安装
node-webkit-agent
你可以通过 npm 安装
node-webkit-agent
到你的项目中,而不是全局安装:npm install --save-dev webkit-devtools-agent
-
启动应用并启用调试代理
在你的 Node.js 应用中,引入
webkit-devtools-agent
并启用它。例如,在你的应用入口文件中添加以下代码:const agent = require('webkit-devtools-agent'); agent.start(); // 启动 HTTP 服务器 const http = require('http'); http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World\n'); }).listen(8080, '127.0.0.1', () => { console.log(`Server running at http://127.0.0.1:8080/`); });
-
访问调试界面
启动你的 Node.js 应用后,你会看到类似如下的输出:
Server running at http://127.0.0.1:8080/ webkit-devtools-agent: Websockets service started on 0.0.0.0:9999
接下来,打开浏览器并访问:
http://localhost:9999/inspector.html?host=localhost:9999&page=0
这将打开一个类似于 Chrome DevTools 的调试界面。
-
调试
在该调试界面中,你可以像在 Chrome DevTools 中一样进行 DOM 操作、查看网络请求、设置断点等操作。
注意事项
node-webkit-agent
目前可能只支持某些特定的功能,如性能分析(Profiles)。- 如果遇到无法启用调试的情况,可以检查是否已正确启动应用并确保没有其他错误阻止调试代理正常工作。
通过上述步骤,你应该能够使用 node-webkit-agent
来调试你的 Node.js 应用。如果遇到任何问题,建议查阅官方文档或寻求社区帮助。
node-webkit-agent
是一个可以让开发者使用 Chrome 开发者工具(DevTools)远程调试 Node.js 应用的工具。以下是如何使用它的简明步骤和示例代码。
首先确保安装了正确的库:
npm install webkit-devtools-agent
接下来,你可以通过以下代码运行你的 Node.js 应用:
const agent = require('webkit-devtools-agent');
const http = require('http');
// 启动 HTTP 服务器
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, '127.0.0.1');
console.log(`[${process.pid}] Server running at http://127.0.0.1:8080/`);
// 发送 SIGUSR2 信号给进程,以启动 DevTools 代理
process.on('SIGUSR2', () => {
agent.start();
});
运行这个应用后,通过发送 SIGUSR2
信号给应用进程来激活代理服务:
kill -SIGUSR2 <进程ID>
然后,打开浏览器访问以下 URL:
http://c4milo.github.io/node-webkit-agent/<chrome-version>/inspector.html?host=localhost:9999&page=0
请替换 <chrome-version>
为你的 Chrome 版本号,并确保 9999
端口上能接收到来自 Chrome DevTools 的 WebSocket 请求。
此链接将引导你进入 Chrome DevTools,让你能够像调试前端一样调试 Node.js 代码。但是请注意,node-webkit-agent
目前只支持某些功能(如性能分析器),对于完整的调试功能可能还不支持。
这只是一个基本的设置,更详细的调试可能需要进一步配置和了解 Chrome DevTools 的使用方法。