日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

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

發(fā)布時(shí)間:2023/11/29 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nodejs调试ndb_如何开始使用NDB调试NodeJS应用程序 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

nodejs調(diào)試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年前發(fā)布了。 NodeJ的默認(rèn)調(diào)試過(guò)程(閱讀Node.js)非常笨拙。 您可能已經(jīng)意識(shí)到需要使用節(jié)點(diǎn)檢查器將--inspect添加到節(jié)點(diǎn)腳本中。 它還依賴于Chrome。 然后,您必須查看正確的Web套接字連接(很難),然后使用Chrome的節(jié)點(diǎn)調(diào)試器進(jìn)行調(diào)試。 老實(shí)說(shuō),這是一個(gè)脖子痛。

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發(fā)布了ndb ,他們稱其為“由Chrome DevTools啟用的Node.js的改進(jìn)調(diào)試體驗(yàn)”。 在調(diào)試Node.js應(yīng)用程序時(shí),Ndb是一個(gè)福音。

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調(diào)試nodejs應(yīng)用程序。 在下面您可以看到ndb的運(yùn)行情況。 現(xiàn)在讓我們卷起袖子開(kāi)始吧:

先決條件 (Prerequisites)

Below are some prerequisites before you get started:

以下是開(kāi)始之前的一些先決條件:

  • You have nodejs installed on your system (a no-brainer but still worth a mention)

    您已經(jīng)在系統(tǒng)上安裝了nodejs(雖然很輕松,但是仍然值得一提)
  • You have general knowledge of running node scripts and working with nodejs apps.

    您具有運(yùn)行節(jié)點(diǎn)腳本和使用nodejs應(yīng)用程序的一般知識(shí)。
  • You have prior debugging experience with nodejs or any other language.

    您具有使用Node.js或任何其他語(yǔ)言進(jìn)行調(diào)試的經(jīng)驗(yàn)。
  • 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.

    為了調(diào)試nodejs應(yīng)用程序,我將使用完整的nodejs express應(yīng)用程序來(lái)代替另一個(gè)腳本。 這是一個(gè)開(kāi)放源代碼應(yīng)用程序,我用于測(cè)試Node.js應(yīng)用程序的演示。

    調(diào)試Node.js Express應(yīng)用程序?yàn)檠菔?(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.

    我正在使用我的開(kāi)源貨幣API來(lái)逐步調(diào)試Node.js應(yīng)用程序。 它是使用ExpressJS框架構(gòu)建的。 您還可以查看Zeit Now上托管的正在運(yùn)行的應(yīng)用程序,以2019-01-10美元對(duì)澳元的匯率為例。

    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.

    該應(yīng)用程序的想法很簡(jiǎn)單。 如果數(shù)據(jù)庫(kù)中有轉(zhuǎn)換率,它將從數(shù)據(jù)庫(kù)中獲取轉(zhuǎn)換率。 如果不是,它將從另一個(gè)API獲取它,并將其發(fā)送回用戶,同時(shí)還將速率保存在數(shù)據(jù)庫(kù)中(異步),以備后用。

    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克隆該應(yīng)用程序,然后運(yùn)行npm install進(jìn)行調(diào)試。 這是一個(gè)非常簡(jiǎn)單的應(yīng)用程序,具有exchangeRates.js 文件中的大多數(shù)邏輯。 它還具有mocha 測(cè)試,因?yàn)樗怯糜跍y(cè)試nodejs應(yīng)用程序的演示。

    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非常容易。 開(kāi)始調(diào)試nodejs應(yīng)用程序所需要做的就是安裝ndb 。 我建議您通過(guò)以下方式全局安裝:

    # 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.

    如果需要,您還可以在每個(gè)應(yīng)用程序本地安裝和使用它。 我必須解決的一件事是獲取最新版本的Chrome,因?yàn)槲铱吹搅艘恍?quán)限問(wèn)題。

    2.使用ndb(而不是node或nodemon)運(yùn)行應(yīng)用程序 (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調(diào)試nodejs應(yīng)用程序,可以直接使用ndb而不是node運(yùn)行nodejs應(yīng)用程序腳本。 例如,如果您nodemon index.js在開(kāi)發(fā)中執(zhí)行node index.js或nodemon index.js 。 要調(diào)試您的應(yīng)用,您可以運(yùn)行:

    ndb index.js

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

    請(qǐng)注意,您不需要放任何東西-- inspect一下,這樣體驗(yàn)會(huì)更加順暢。

    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!

    您無(wú)需記住其他端口,也無(wú)需進(jìn)入chrome devtools并打開(kāi)其他檢查器窗口即可進(jìn)行調(diào)試。 這么解脫!

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

    執(zhí)行ndb時(shí),ndb會(huì)打開(kāi)如下屏幕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:

    請(qǐng)?jiān)诘?6行添加一個(gè)斷點(diǎn)。使用ndb運(yùn)行應(yīng)用程序后,它將在調(diào)試模式下運(yùn)行,并在您擊中http://localhost:8080/api/convert/USD/AUD/2019-01-01時(shí)在以下斷點(diǎn)處停止瀏覽器上的http://localhost:8080/api/convert/USD/AUD/2019-01-01 。 我在下面的屏幕截圖中在exchangeRates.js上設(shè)置了斷點(diǎn),例如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允許您運(yùn)行任何腳本進(jìn)行調(diào)試。 例如,我可以運(yùn)行ndb npm start ,它將使用nodemon運(yùn)行。 這意味著我可以在更改代碼的同時(shí)調(diào)試應(yīng)用程序,這很棒。

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

    作為示例,它可以與ndb npm start一起運(yùn)行以調(diào)試此nodejs express應(yīng)用程序。

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

    您還可以使用ndb npm test類的命令調(diào)試ndb npm test 。

    3.讓我們調(diào)試一些代碼 (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.

    當(dāng)調(diào)試器正在工作時(shí),我可以按自己的速度和便利性放置更多的斷點(diǎn)或遍歷代碼。

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

    基本的快捷鍵是F10跳過(guò)功能調(diào)用和F11進(jìn)入功能。

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

    我認(rèn)為您熟悉的常規(guī)調(diào)試工作流程。 下面我進(jìn)入第52行:

    更多調(diào)試內(nèi)容 (More debugging things)

    As with any other debugger, with ndb you can:

    與其他調(diào)試器一樣,使用ndb,您可以:

  • Add watches

    新增手表
  • Check the call stack trace

    檢查調(diào)用堆棧跟蹤
  • Check the process

    檢查過(guò)程
  • The console tab is also helpful if you want to some quick nodejs code in the context.

    如果您想在上下文中使用一些快速的nodejs代碼,則控制臺(tái)選項(xiàng)卡也很有用。

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

    在官方自述文件中了解有關(guān)使用ndb可以做什么的更多信息。 以下是有用的控制臺(tái)的屏幕截圖:

    結(jié)論(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調(diào)試任何nodejs應(yīng)用程序都是更好的開(kāi)發(fā)人員體驗(yàn)。 要使用ndb調(diào)試貨幣API nodejs express應(yīng)用,請(qǐng)運(yùn)行以下命令,前提是已安裝節(jié)點(diǎn)> 8:

  • npm install -g ndb

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

    git clone [受電子郵件保護(hù)] :geshan / currency-api.git

  • cd currency-api

    cd currency-api
  • npm install

    npm安裝
  • ndb npm start

    ndb npm開(kāi)始
  • After the ndb debugger opens up, add a breakpoint at line 46 of src/exchangeRates.js

    打開(kāi)ndb調(diào)試器后,在src / exchangeRates.js的第46行添加一個(gè)斷點(diǎn)
  • Then open http://localhost:8080/api/convert/USD/AUD/2019-01-01 in the browser

    然后在瀏覽器中打開(kāi)http://localhost:8080/api/convert/USD/AUD/2019-01-01

  • Now as the app should pause at the breakpoint, enjoy! and continue debugging.

    現(xiàn)在,由于該應(yīng)用程序應(yīng)在斷點(diǎn)處暫停,請(qǐng)盡情享受吧! 并繼續(xù)調(diào)試。
  • If it works for this app, you can debug any of your nodejs application with this approach.

    如果適用于此應(yīng)用程序,則可以使用此方法調(diào)試任何nodejs應(yīng)用程序。

    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.

    歡迎使用調(diào)試nodejs應(yīng)用程序的新方法,該方法與瀏覽器無(wú)關(guān),并且比默認(rèn)體驗(yàn)平滑得多。 加強(qiáng)您的調(diào)試nodejs應(yīng)用程序游戲。

    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!

    希望本文有助于您更好地調(diào)試Node.js應(yīng)用程序。 如果您還有其他關(guān)于調(diào)試Node.js應(yīng)用程序或更好地使用ndb的信息,請(qǐng)?jiān)谙旅嬖u(píng)論!

    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調(diào)試ndb

    總結(jié)

    以上是生活随笔為你收集整理的nodejs调试ndb_如何开始使用NDB调试NodeJS应用程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。