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的运行情况。 现在让我们卷起袖子开始吧:
Below are some prerequisites before you get started:
以下是开始之前的一些先决条件:
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应用程序的演示。
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应用程序的演示。
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,因为我看到了一些权限问题。
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.js
或nodemon 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
。
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行:
As with any other debugger, with ndb you can:
与其他调试器一样,使用ndb,您可以:
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可以做什么的更多信息。 以下是有用的控制台的屏幕截图:
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:
git clone [email protected]:geshan/currency-api.git
git clone [受电子邮件保护] :geshan / currency-api.git
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
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