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.pswp.cn/news/392582.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392582.shtml
英文地址,請注明出處:http://en.pswp.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…