nodejs调试ndb_如何开始使用NDB调试NodeJS应用程序

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 ndb

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

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

  3. cd currency-api

    cd currency-api
  4. npm install

    npm安装
  5. ndb npm start

    ndb 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392582.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

初学必读:61条面向对象设计的经验原则

(1)所有数据都应该隐藏在所在的类的内部。(2)类的使用者必须依赖类的共有接口,但类不能依赖它的使用者。(3)尽量减少类的协议中的消息。(4)实现所有类都理解的最基本公有接口[例如,拷贝操作(深拷贝和浅拷贝)、相等性判断、正确输出内容、从ASCII描述解析…

栈,递归

栈的基本操作注意&#xff1a;是从后往前连接的 1 #include <stdio.h>2 #include <Windows.h>3 typedef struct sStack4 {5 int num;6 struct sStack* pnext;7 }Stack;8 void push(Stack **pStack,int num);9 int pop(Stack **pStack); 10 BOOL isEmpty(St…

mysql集群多管理节点_项目进阶 之 集群环境搭建(三)多管理节点MySQL集群

多管理节点MySQL的配置很easy&#xff0c;仅须要改动之前的博文中提高的三种节点的三个地方。1)改动管理节点配置打开管理节点C:\mysql\bin下的config.ini文件&#xff0c;将当中ndb_mgmd的相关配置改动为例如以下内容&#xff1a;[ndb_mgmd]# Management process options:# Ho…

leetcode 767. 重构字符串(贪心算法)

给定一个字符串S&#xff0c;检查是否能重新排布其中的字母&#xff0c;使得两相邻的字符不同。 若可行&#xff0c;输出任意可行的结果。若不可行&#xff0c;返回空字符串。 示例 1: 输入: S “aab” 输出: “aba” 代码 class Solution {public String reorganizeStri…

APK伪加密

一、伪加密技术原理 我们知道android apk本质上是zip格式的压缩包&#xff0c;我们将android应用程序的后缀.apk改为.zip就可以用解压软件轻松的将android应用程序解压缩。在日常生活或者工作中&#xff0c;我们通常为了保护我们自己的文件在进行压缩式都会进行加密处理。这样的…

乱花渐欲迷人眼-杜绝设计的视噪

视噪&#xff0c;又称视觉噪音。我们每天接受来自外界的大量信息&#xff0c;这些信息有将近70&#xff05;是通过视觉感知获得的。视噪会干扰我们对信息的判断&#xff0c;影响到产品的易用性和可用性&#xff0c;与用户体验的好坏息息相关。(克劳德香农图演示了噪音如何影响信…

超详细windows安装mongo数据库、注册为服务并添加环境变量

1.官网下载zip安装包 官网地址https://www.mongodb.com/download-center/community?jmpnav&#xff0c;现在windows系统一般都是64位的&#xff0c;选好版本、系统和包类型之后点击download&#xff0c;mongodb-win32-x86_64-2008plus-ssl-4.0.10.zip。 2.解压zip包&#xff0…

开源 数据仓库_使用这些开源工具进行数据仓库

开源 数据仓库by Simon Spti西蒙斯派蒂(SimonSpti) 使用这些开源工具进行数据仓库 (Use these open-source tools for Data Warehousing) These days, everyone talks about open-source software. However, this is still not common in the Data Warehousing (DWH) field. W…

.netcore mysql_.netcore基于mysql的codefirst

.netcore基于mysql的codefirst此文仅是对于netcore基于mysql的简单的codefirst实现的简单记录。示例为客服系统消息模板的增删改查实现第一步、创建实体项目&#xff0c;并在其中建立对应的实体类&#xff0c;以及数据库访问类须引入Pomelo.EntityFrameworkCore.MySql和Microso…

leetcode 34. 在排序数组中查找元素的第一个和最后一个位置(二分查找)

给定一个按照升序排列的整数数组 nums&#xff0c;和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。 如果数组中不存在目标值 target&#xff0c;返回 [-1, -1]。 进阶&#xff1a; 你可以设计并实现时间复杂度为 O(log n) 的算法解决此问题吗&#xff1…

CentOS6.7上使用FPM打包制作自己的rpm包

自定义rpm包&#xff0c;还是有逼格和实际生产环境的意义的。 (下面的文档有的代码由于博客排版的问题导致挤在了一起&#xff0c;需要自己判别&#xff09; 安装FPM fpm是ruby写的&#xff0c;因此系统环境需要ruby&#xff0c;且ruby版本号大于1.8.5。 # 安装ruby模块 yum -y…

汉堡菜单_开发人员在编写汉堡菜单时犯的错误

汉堡菜单by Jared Tong汤杰(Jared Tong) 开发人员在编写汉堡菜单时犯的错误 (The mistake developers make when coding a hamburger menu) What do The New York Times’ developers get wrong about the hamburger menu, and what do Disney’s and Wikipedia’s get right?…

android 涨潮动画加载_Android附带涨潮动画效果的曲线报表绘制

写在前面本文属于部分原创&#xff0c;实现安卓平台正弦曲线类报表绘制功能介绍&#xff0c;基于网络已有的曲线报表绘制类(LineGraphicView)自己添加了涨潮的渐变动画算法最终效果图废话少说&#xff0c;直接上源码一、自定义View LineGraphicView&#xff0c;本类注释不算多&…

使用css3属性transition实现页面滚动

<!DOCTYPE html> <html><head><meta http-equiv"Content-type" content"text/html; charsetutf-8" /><title>慕课七夕主题</title><script src"http://libs.baidu.com/jquery/1.9.1/jquery.min.js">&…

leetcode 321. 拼接最大数(单调栈)

给定长度分别为 m 和 n 的两个数组&#xff0c;其元素由 0-9 构成&#xff0c;表示两个自然数各位上的数字。现在从这两个数组中选出 k (k < m n) 个数字拼接成一个新的数&#xff0c;要求从同一个数组中取出的数字保持其在原数组中的相对顺序。 求满足该条件的最大数。结…

Oracle Study之--Oracle等待事件(5)

Db file single write这个等待事件通常只发生在一种情况下&#xff0c;就是Oracle 更新数据文件头信息时&#xff08;比如发生Checkpoint&#xff09;。当这个等待事件很明显时&#xff0c;需要考虑是不是数据库中的数据文件数量太大&#xff0c;导致Oracle 需要花较长的时间来…

两台centos之间免密传输 scp

两台linux服务器之间免密scp&#xff0c;在A机器上向B远程拷贝文件 操作步骤&#xff1a;1、在A机器上&#xff0c;执行ssh-keygen -t rsa&#xff0c;一路按Enter&#xff0c;不需要输入任何内容。&#xff08;如有提示是否覆盖&#xff0c;可输入y后按回车&#xff09;2、到/…

jsp导出数据时离开页面_您应该在要离开的公司开始使用数据

jsp导出数据时离开页面If you’re new in data science, “doing data science” likely sounds like a big deal to you. You might think that you need meticulously collected data, all the tools for data science and a flawless knowledge before you can claim that y…

分步表单如何实现 html_HTML表格入门的分步指南

分步表单如何实现 htmlby Abhishek Jakhar通过阿比舍克贾卡(Abhishek Jakhar) HTML表格入门的分步指南 (A step-by-step guide to getting started with HTML tables) 总览 (Overview) The web is filled with information like football scores, cricket scores, lists of em…

laravel mysql pdo,更改Laravel中的基本PDO配置

My shared web host have some problems with query prepares and I want to enable PDOs emulated prepares, theres no option for this in the config\database.php.Is there any way I can do that in Laravel?解决方案You can add an "options" array to add o…