Node.js小記錄
打開terminal,確認node安裝
查詢node.js版本:node -v
查詢npm版本:npm -v
進入node:node
離開node:ctrl+C
Node Package Manager(npm)
- 除了官方核心模組之外可以透過npm安裝他人製作的模組
node.js安裝檔裡有包含npm
- 自行建立package.json(專案設定檔)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
在cmd內輸入npm init
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
npm可以經由json的設定載入專案所需的模組
完成後會在路徑下自動產生package.json
安裝npm的express模組
- express.js是node.js的核心模組之一,用於快速建立網頁架構
在專案路徑下開啟terminal
npm list
顯示已安裝的npm
npm uninstall {模組名稱}
刪除專案內的npm
--save
會建立模組專用資料夾node_modules並更新 package.json 內 "dependencies" 的資訊
--save-dev
會在 package.json 額外新增一組除錯專用的模組設定 "devDependencies" 的資訊
-g
全域安裝,將模組安裝在最底層路徑且直接套用本機所有專案,方便但會造成模組管理困難
windows: X:\Users\username\AppData\Romign\npm\node_modules
mac: /usr/local/lib/node_modules
windows : npm install express --save
mac : sudo npm install express --save
最後在 package.json 關聯的檔案內用 const {變數名}=require('express');
呼叫模組
nodemon 模組:只要code存檔時內容產生變化,會自動重新執行檔案
一般在使用他人專案時會透過專案內的 package.json 來安裝專案內所需要的模組
在專案路徑下用cmd執行npm install
關於modules版本號
有些package.json會對版本號做相關設定
範例: "JQuery":"^3.6.0"
- 3 =>主要版本號
- 6 =>次要版本號
- 0 =>bug修正
有些版本號前面會加上 ^ 或是 ~
^:自動更新次版本 3.x.x
~:只會自動更新bug修正 3.6.x
latest:我全都要最新搭!
global 全域變數 > var 函式區域變數 > let 區塊變數
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
require & module.exports
- require('檔案路徑') :從A.js呼叫B.js物件的語法
- module.exports :將想傳給A.js的資料用此語法輸出
兩種寫法通常不會同時出現,若發生後者會完全覆蓋前者的屬性
假設我已經下了3個獨立屬性
export.AAA=data;
export.BBB='valueB';
export.CCC='valueC';
但我後來又用
module.exports={AAA: data};
則最後只會殘留AAA:data;
輸出結果:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
HTTP Module
- 使用node執行後在瀏覽器輸入127.0.0.1:{listen的port}即可運作
JSON格式轉換為字串顯示
JSON.stringify({json格式})
檢查陣列是否從「字串」起始,是則true,否則false
- 陣列.startWith('字串')
將字串從以keyword為界,切割為陣列型態
- 字串.split('要切割的keyword')
刪除陣列的最後一個元素並刪除它
- 陣列.pop()
從陣列的指定位置刪除n個元素後增加元素
- 陣列.splice(從第index個位置開始,刪除n個,新增什麼元素)
PATH Module
- 可以透過 var {變數名稱} = require('path'); 載入path模組
- __dirname: 專案資料夾所在路徑
- __filename: 檔案所在路徑
Todolist RESTful API kata
postman 下載
JSON Formatter:chrome格式化json檔的工具
JSONVue :功能同上
Universally Unique Identifier(UUID、通用唯一辨識碼)
類似身份證字號的東西,讓資訊具有唯一性
npm uuid install
try catch
- 類似if-else的東西,request的資料或物件沒有滿足try的格式則輸出catch內容
preflight options API:跨網域預檢機制
- method類型為option。指有跨網域的請求行為時,先行確認網域狀態再做第二次行為請求。
大概就是第一次request會檢查來源是不是有鬼,第二次才會正式傳送資料。
heroku環境設置
修改package.json內容
登入heroku CLI
heroku login
建立一個heroku雲端主機
heroku create
此時heroku會自動建立一個remote
部屬git環境
cmd輸入git init
新增.gitignore,輸入內容node_modules/,目的是對npm版控
後面就是一般的git上傳add/commit/push
MongoDB & compass-GUI資料庫軟體 windows