iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 5
0
自我挑戰組

從零開始的後端異世界生活系列 第 5

Day 05 - 簡易 NestJS 專案導覽(上) 與引入 Dependencies

簡易 NestJS 專案導覽

在上上篇章的時候,我們透過指令「nest new nestjs-task-management」建立了我們的第一個 NestJS 專案,使用 VSCode 將專案給打開後,檔案結構大概會長這樣子:

https://ithelp.ithome.com.tw/upload/images/20200920/201196195qyXDXKyFD.png

身為一個初碰後端的人,看到這樣沒看過的場景,當然會先懵逼了一下。

那這裡就先來「簡易」的介紹一下,真的是非常「簡易」:

  • node_modules
    https://ithelp.ithome.com.tw/upload/images/20200920/201196192eLfd4Zups.png

各式各樣 Package,應該暫時不用理它。

  • src
    https://ithelp.ithome.com.tw/upload/images/20200920/20119619IaiG5beyOT.png

開發後端程式碼的主要區塊。

  • test
    https://ithelp.ithome.com.tw/upload/images/20200920/201196194N3thQMWBP.png

執行測試的區塊。

  • package.json(其他則是一些執行要用的設定檔,有用到再解說)
    https://ithelp.ithome.com.tw/upload/images/20200920/20119619iP2Be14eUj.png

https://ithelp.ithome.com.tw/upload/images/20200920/201196197qoKs1JvwD.png

專案設定、執行腳本、要引入的 Dependencies

例如,我想 Run 起我的後端伺服器看看,使用內建的 Scripts:
https://ithelp.ithome.com.tw/upload/images/20200920/20119619la9DBDdDUY.png

yarn start:dev

就會以 watch mode 在本地端運營起你的 Server

https://ithelp.ithome.com.tw/upload/images/20200920/20119619NkbqkI5X9g.png

此時若沒有一些錯誤發生,在瀏覽器輸入 http://localhost:3000 應該是可以順利看到「Hello World!」。

引入好用的 Dependencies

所謂「前人造樹,後人乘涼」,在開發中常常會遇到一些功能是別人已經寫好、造好輪子的,我們就不必自己再苦苦煩惱是要如何寫出那樣的東西,將它引入我們的專案中,直接使用它!

在這裡我們要當作範例的是「Swagger UI」,在官方文件亦可找到它的介紹,若是使用 npm 的朋友可以照官方輸入:
https://ithelp.ithome.com.tw/upload/images/20200920/20119619qPKXgZZqQt.png

而我自己使用 yarn,則輸入:

yarn add @nestjs/swagger swagger-ui-express -S

此時在 package.json 就會出現我們安裝的 Dependencies:
https://ithelp.ithome.com.tw/upload/images/20200920/201196197oxA7bAb9H.png

來隨意寫寫試試看是否有引入成功,使用 swagger 相關功能
https://ithelp.ithome.com.tw/upload/images/20200920/201196196U8vhLsYx0.png

Yes,有順利引入。
https://ithelp.ithome.com.tw/upload/images/20200920/20119619kioJeFYxK9.png

(後續會有篇章繼續介紹 Swagger 相關使用方法)

/*
本系列文係個人新手開發心得,可能會有許多錯誤,煩請多多包容不吝指教。
*/


上一篇
Day 04 - 開啟方便的引入相對路徑設定
下一篇
Day 06 - 簡易 NestJS 專案導覽(下) 與你的第一支 API
系列文
從零開始的後端異世界生活30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言