分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

如何 debug nodeJS 代码? how to inspect NodeJS code?

发布时间:2023-09-06 02:35责任编辑:彭小芳关键词:debugNode

基本原理

  1. 启动 WebSocket 进行基于 inspect protocol 双工通信
    1. current_node_process <=> current_websocket <=> current_inspect_tool
  2. 启动 HTTP 服务,记录源(Meta)信息
  3. 每个程序会生成一个 UUID

IDE

VS Code

VS code 中打断点,然后进入 debugger 面板,点击开始。

Config launch.json

mkdir node-debug-democd node-debug-demotouch cmd.jsecho "console.log(process.argv.slice(2))" >> cmd.jsnode cmd.js -t test -a abort

Will show folling results:

[ '-t', ?'test', ?'-a', ?'abort' ]

But may they never been show for VS code debug mode, so we need canfig the .vscode/launch.json:

Then we could see this:

We could write manaul or auto generate new:

Code:

{ ?"type": "node", ?"request": "launch", ?"name": "Test cmd.js", ?"program": "${workspaceFolder}/cmd.js", ?"args": [ ???"-t", ???"test", ???"-a", ???"abort" ?]}

Use VS code debug panel to debug the cmd.js before this we must chekcout to it:

Chrome DEVTOOLS

chrome://inspect

1st: Run CMD in terminal and the CLI options are right here:

node debug -help # it is very useful as well# or node inspect dmeo.js> help

2nd: Navigates to chrome://inspect page

inspect protocol

1st: Run CMD in terminal and the CLI options are right here:

复制 HOST + PORT 端口号进入浏览器:

HOST + PORT + "/json" 查看 HTTP

复制返回的 JSON 字符串中的 devtoolsFrontendUrl 字段,打上 breakpoint, 开始调试:

DEVTOOLS Node ICON

only works for OSX and Linux.
点击绿色的 nodeJS ICON.

性能调优

open DEVTOOLS - JSProfiler recording, then refresh current page to record how long used of each function execute.

END

Have fun with NodeJS.

如何 debug nodeJS 代码? how to inspect NodeJS code?

原文地址:https://www.cnblogs.com/givingwu/p/10569056.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved