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:
以下是開始之前的一些先決條件:
- You have nodejs installed on your system (a no-brainer but still worth a mention) 您已經在系統上安裝了nodejs(雖然很輕松,但是仍然值得一提)
- You have general knowledge of running node scripts and working with nodejs apps. 您具有運行節點腳本和使用nodejs應用程序的一般知識。
- 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.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
。
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,您可以:
- Add watches 新增手表
- Check the call stack trace 檢查調用堆棧跟蹤
- 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:
- npm install -g ndb npm install -g ndb
git clone [email protected]:geshan/currency-api.git
git clone [受電子郵件保護] :geshan / currency-api.git
- cd currency-api cd currency-api
- npm install npm安裝
- ndb npm start ndb npm開始
- After the ndb debugger opens up, add a breakpoint at line 46 of src/exchangeRates.js 打開ndb調試器后,在src / exchangeRates.js的第46行添加一個斷點
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
- 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