nodejs调试ndb

NodeJs was released almost 9 years ago. The default debugging process of NodeJs (read Node.js) is quite clumsy. You are likely already aware of the need to add --inspect to the node script with node inspector. It is also dependent on Chrome. Then you have to look at the proper web socket connection which is hard, and debug using Chrome’s node debugger. To be honest, it is a pain in the neck.

NodeJ大约9年前发布了。 NodeJ的默认调试过程(阅读Node.js)非常笨拙。 您可能已经意识到需要使用节点检查器将--inspect添加到节点脚本中。 它还依赖于Chrome。 然后,您必须查看正确的Web套接字连接(很难),然后使用Chrome的节点调试器进行调试。 老实说,这是一个脖子痛。

Finally, Google chromelabs has released ndb, which they say is “An improved debugging experience for Node.js, enabled by Chrome DevTools”. Ndb is a boon when debugging a Nodejs app.

最后,谷歌chromelabs发布了ndb ,他们称其为“由Chrome DevTools启用的Node.js的改进调试体验”。 在调试Node.js应用程序时,Ndb是一个福音。

I am going to show a step by step process of how to debug a nodejs application with ndb. Below you can see ndb in action. So now let’s roll up our sleeves and get started:

我将逐步演示如何使用ndb调试nodejs应用程序。 在下面您可以看到ndb的运行情况。 现在让我们卷起袖子开始吧:

先决条件 (Prerequisites)

Below are some prerequisites before you get started:

以下是开始之前的一些先决条件:

  1. You have nodejs installed on your system (a no-brainer but still worth a mention)您已经在系统上安装了nodejs(虽然很轻松,但是仍然值得一提)
  2. You have general knowledge of running node scripts and working with nodejs apps.您具有运行节点脚本和使用nodejs应用程序的一般知识。
  3. You have prior debugging experience with nodejs or any other language.您具有使用Node.js或任何其他语言进行调试的经验。

For debugging nodejs applications, in place of just another script I will use a full nodejs express application. It is an open source application I used for a demo on testing nodejs applications.

为了调试nodejs应用程序,我将使用完整的nodejs express应用程序来代替另一个脚本。 这是一个开放源代码应用程序,我用于测试Node.js应用程序的演示。

调试Node.js Express应用程序为演示 (Debugging nodejs express application as a demo)

I am using my open source currency API for this step-by-step guide to debugging a nodejs application. It is built using the ExpressJS framework. You can also check out the running app hosted on Zeit Now to see the USD to AUD rate of 2019–01–10 as an example.

我正在使用我的开源货币API来逐步调试Node.js应用程序。 它是使用ExpressJS框架构建的。 您还可以查看Zeit Now上托管的正在运行的应用程序,以2019-01-10美元对澳元的汇率为例。

The idea of the application is simple. If the conversion rate is available in the database, it will fetch it from the database. If not, it will fetch it from another API and send it back to the user, also saving the rate in the database at the same time (async) for later use.

该应用程序的想法很简单。 如果数据库中有转换率,它将从数据库中获取转换率。 如果不是,它将从另一个API获取它,并将其发送回用户,同时还将速率保存在数据库中(异步),以备后用。

You can clone the application from github and run npm install to get it ready for debugging. This is a very simple application with most of the logic in exchangeRates.js file. It has mocha tests too as it was a demo for testing a nodejs application.

您可以从github克隆该应用程序,然后运行npm install进行调试。 这是一个非常简单的应用程序,具有exchangeRates.js 文件中的大多数逻辑。 它还具有mocha 测试,因为它是用于测试nodejs应用程序的演示。

1.入门,安装ndb (1. Getting started, install ndb)

Installing ndb is very easy. All you need to do to get started debugging your nodejs application is to install ndb. I would suggest that you install it globally with:

安装ndb非常容易。 开始调试nodejs应用程序所需要做的就是安装ndb 。 我建议您通过以下方式全局安装:

# with npm
npm install -g ndb
# with yarn
yarn global add ndb

You can also install and use it locally per app if you want. One thing I had to fix was to get the latest version of Chrome, as I saw some permission issues.

如果需要,您还可以在每个应用程序本地安装和使用它。 我必须解决的一件事是获取最新版本的Chrome,因为我看到了一些权限问题。

2.使用ndb(而不是node或nodemon)运行应用程序 (2. Run the app with ndb (not node or nodemon))

For debugging nodejs applications with ndb, you can directly run the nodejs app script with ndb rather than node. For example, if you were used to doing node index.js or nodemon index.js in development. To debug your app you can run:

要使用ndb调试nodejs应用程序,可以直接使用ndb而不是node运行nodejs应用程序脚本。 例如,如果您nodemon index.js在开发中执行node index.jsnodemon index.js 。 要调试您的应用,您可以运行:

ndb index.js

Notice that you don’t need to put any -- inspect so the experience is a lot smoother.

请注意,您不需要放任何东西-- inspect一下,这样体验会更加顺畅。

You don’t need to remember a different port or go to chrome devtools and open up a different inspector window to debug. Such a relief!

您无需记住其他端口,也无需进入chrome devtools并打开其他检查器窗口即可进行调试。 这么解脱!

ndb opens up a screen like below when you do ndb . or ndb index.js:

执行ndb时,ndb会打开如下屏幕ndb .ndb index.js

Please add a breakpoint on line 46. As you have run the application with ndb it will run in debug mode and stop at the breakpoint like below when you hit http://localhost:8080/api/convert/USD/AUD/2019-01-01 on the browser. I have set the breakpoint on exchangeRates.js like 46 in the screenshot below:

请在第46行添加一个断点。使用ndb运行应用程序后,它将在调试模式下运行,并在您击中http://localhost:8080/api/convert/USD/AUD/2019-01-01时在以下断点处停止浏览器上的http://localhost:8080/api/convert/USD/AUD/2019-01-01 。 我在下面的屏幕截图中在exchangeRates.js上设置了断点,例如46:

ndb allows you to run any script for debugging. For example, I can run ndb npm start and it will use the nodemon run. This means I can debug the application while changing the code which is great.

ndb允许您运行任何脚本进行调试。 例如,我可以运行ndb npm start ,它将使用nodemon运行。 这意味着我可以在更改代码的同时调试应用程序,这很棒。

As an example it can be run with ndb npm start to debug this nodejs express application.

作为示例,它可以与ndb npm start一起运行以调试此nodejs express应用程序。

You can also debug your test with a command like ndb npm test.

您还可以使用ndb npm test类的命令调试ndb npm test

3.让我们调试一些代码 (3. Let’s debug some code)

As the debugger is working I can place more break points or run through the code at my speed and convenience.

当调试器正在工作时,我可以按自己的速度和便利性放置更多的断点或遍历代码。

The essential shortcuts are F10 to step over function call and F11 to step into a function.

基本的快捷键是F10跳过功能调用和F11进入功能。

The usual debugging workflow I assume you are familiar with. Below I have advanced to line 52:

我认为您熟悉的常规调试工作流程。 下面我进入第52行:

更多调试内容 (More debugging things)

As with any other debugger, with ndb you can:

与其他调试器一样,使用ndb,您可以:

  1. Add watches新增手表
  2. Check the call stack trace检查调用堆栈跟踪
  3. Check the process检查过程

The console tab is also helpful if you want to some quick nodejs code in the context.

如果您想在上下文中使用一些快速的nodejs代码,则控制台选项卡也很有用。

Read more about what you can do with ndb in the official readme. Below is a screenshot of the useful console:

在官方自述文件中了解有关使用ndb可以做什么的更多信息。 以下是有用的控制台的屏幕截图:

结论(TL; DR) (Conclusion (TL;DR))

Debugging any nodejs application with ndb is a better developer experience. To debug the currency API nodejs express app with ndb, you run the following commands, given you have node > 8 installed:

使用ndb调试任何nodejs应用程序都是更好的开发人员体验。 要使用ndb调试货币API nodejs express应用,请运行以下命令,前提是已安装节点> 8:

  1. npm install -g ndbnpm install -g ndb
  2. git clone [email protected]:geshan/currency-api.git

    git clone [受电子邮件保护] :geshan / currency-api.git

  3. cd currency-apicd currency-api
  4. npm installnpm安装
  5. ndb npm startndb npm开始
  6. After the ndb debugger opens up, add a breakpoint at line 46 of src/exchangeRates.js打开ndb调试器后,在src / exchangeRates.js的第46行添加一个断点
  7. Then open http://localhost:8080/api/convert/USD/AUD/2019-01-01 in the browser

    然后在浏览器中打开http://localhost:8080/api/convert/USD/AUD/2019-01-01

  8. Now as the app should pause at the breakpoint, enjoy! and continue debugging.现在,由于该应用程序应在断点处暂停,请尽情享受吧! 并继续调试。

If it works for this app, you can debug any of your nodejs application with this approach.

如果适用于此应用程序,则可以使用此方法调试任何nodejs应用程序。

Welcome to the new way of debugging nodejs applications that is browser independent and a lot smoother than the default experience. Step up your debugging nodejs application game.

欢迎使用调试nodejs应用程序的新方法,该方法与浏览器无关,并且比默认体验平滑得多。 加强您的调试nodejs应用程序游戏。

I hope this post has helped you debug your nodejs application better. If you have any other things to share about debugging nodejs apps or better usage of ndb please comment below!

希望本文有助于您更好地调试Node.js应用程序。 如果您还有其他关于调试Node.js应用程序或更好地使用ndb的信息,请在下面评论!

Thanks for reading!

谢谢阅读!

You can read more of my blog posts geshan.com.np.

您可以阅读我的更多博客文章geshan.com.np 。

翻译自: https://www.freecodecamp.org/news/how-to-get-started-debugging-nodejs-applications-with-ndb-a37e8747dbba/

nodejs调试ndb

nodejs调试ndb_如何开始使用NDB调试NodeJS应用程序相关推荐

  1. 使用NDB调试Linux字符设备

    使用NDB调试Linux字符设备 cdev_init 在Linux中字符设备通过cdev结构体来表示,为了观察Linux上面的字符设备的创建过程,就需要在cdev_init处设置断点. 给cdev_i ...

  2. 使用NDB调试网络栈

    使用NDB调试网络栈 __dhd_sendpkt 为了查看bcmdhd的发送网络包,需要先在`__dhd_sendpkt`处设置断点. bl0 e ffffff80`011b45e8 0001 (00 ...

  3. 使用NDB调试Linux内核的线程切换过程

    使用NDB调试Linux内核的线程切换过程 cpu_switch_to 因为要调试Linux内核的线程切换过程,所以需要在`cpu_switch_to`处设置断点. 注意:ARM无法设置软件断点,只能 ...

  4. vconsole 调试 查看LOG VUE在手机上调试 手机查看h5的日志

    简单介绍下vConsole: vConsole是一个由微信公众平台前端团队研发的web前端开发者面板,可用于展示console日志,方便开发.调试. 使用场景1,在vue-cli 构建的项目中使用: ...

  5. ECLIPSE远程调试出现如下问题 ECLIPSE中调试代码提示找不到源

    ECLIPSE远程调试出现如下问题  ECLIPSE中调试代码提示找不到源 如此提示是由于没有JAR包中找不到MAIN,可以手动添加或自己修改 利用ECLIPSE填写JAR中的MAIN方法后,就可以调 ...

  6. 如何使VS2008 调试网站的根目录和IIS调试的一致?

    如何使VS2008 调试网站的根目录和IIS调试的一致? 用VS2008做asp.net网站调试时,经常会多出来一个目录,如http://localhost:1234/Foo/ , 由于一些图片的路径 ...

  7. Bochs调试Linux内核6 - 启动过程调试 - 跳到bootsect引导程序执行

    接此,​​​​​​Bochs调试Linux内核5 - 启动过程调试 - 认识Bootsect.S_bcbobo21cn的专栏-CSDN博客 看一下,0x00007c11 这里是重复执行串传送:而后一条 ...

  8. 软件调试学习笔记(三)—— 调试事件的处理

    软件调试学习笔记(三)-- 调试事件的处理 要点回顾 调试事件的处理 实验一:实现简单调试器(创建进程) 实验二:分析异常来源 实验三:实现简单调试器(附加进程) 实验四:分析NtDebugActiv ...

  9. 软件调试学习笔记(二)—— 调试事件的采集

    软件调试学习笔记(二)-- 调试事件的采集 要点回顾 调试事件的种类 调试事件采集函数 例:分析PspUserThreadStartup 例:分析PspExitThread 总结 要点回顾 调试器与被 ...

最新文章

  1. 「SAP技术」SAP 如何看序列号被包在哪些HU里?
  2. 重构——71将领域和表述/显示分开(Separate Domain from Presentation)
  3. Sublime Text3软件安装教程
  4. 判断子序列不同的子序列两个字符串的删除操作编辑距离
  5. MySQL的一些基础操作
  6. c语言ungetc参数,关于一些C语言标准I/O函数的见解。int ungetc(int ch,FILE *fp)函数...
  7. ubuntu下面制作ubuntu系统启动盘(转载+自己整理)
  8. 使用Jexus服务器运行Asp.Net Core2.0程序
  9. Linux C 中字符串化操作符#
  10. LB负载均衡集群--LVS
  11. 真香!8 行代码搞定最大子数组和问题
  12. 2017黑色主题 server sql_王智好美,一袭黑色亮片吊带长裙,造型简约大气,时尚优雅...
  13. html 悬浮在固定位置,纯CSS实现DIV悬浮(固定位置)
  14. 安装inotify-tools监控工具
  15. KETTLE使用教程
  16. 推荐一款超级好用的开源项目画图工具
  17. 删除树莓派默认密码的SSH警告
  18. VMware14虚拟机破解版安装详细教程
  19. ajax加载txt文件,ajax读取txt文件
  20. android ftp播放器,超强本地播放器一款支持samba、FTP/Windows共享服务-简单不折腾...

热门文章

  1. UNIX网络编程笔记(1):TCP简介
  2. python字符串是用双引号括起来的_用python连接字符串列表并用引号将每个字符串括起来...
  3. Unity3D热更新之LuaFramework篇[06]--Lua中是怎么实现脚本生命周期的
  4. [Apple开发者帐户帮助]八、管理档案(2)创建临时配置文件(iOS,tvOS,watchOS)...
  5. python里的apply,applymap和map的区别
  6. 在使用ToolBar + AppBarLayout,实现上划隐藏Toolbar功能,遇到了一个坑。
  7. java.sql.SQLException: ORA-01438: 值大于此列指定的允许精确度
  8. Xcode做简易计算器
  9. 文本二叉树折半查询及其截取值
  10. ROS project part 1: Ubuntu中安装opencv包以及相应的依赖