tags: Node.js

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 name: (nodejs) //專案名稱 version: (1.0.0) //版本 description: 666 //自訂描述 entry point: (A.js) //關聯的檔案 test command: //測試指令(沒有就空白) git repository: //關聯的git專案 keywords: //這個json檔的關鍵字 author: XimenZangief //作者 license: (ISC) //是否開源

完成後會在路徑下自動產生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物件的語法
// A.js: var bbb = require('./B.js'); console.log(bbb);
  • module.exports :將想傳給A.js的資料用此語法輸出
// B.js: var data=123; // 可以使用數字、字串、陣列、元件、甚至函式型態傳值 module.exports={ AAA: data, BBB: 'valueB', CCC: 'valueC', }; // 相同寫法,且元件內屬性各自獨立 exports.AAA =data; exports.BBB ='valueB'; exports.CCC ='valueC'; };

兩種寫法通常不會同時出現,若發生後者會完全覆蓋前者的屬性
假設我已經下了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}即可運作
const http= require('http'); // user發出的請求, server的回應 http.createServer(function (request, response){ //response狀態碼 // 回傳的文件格式 response.writeHead(200,{"Content-Type":"text/plain"}); response.write('hello'); response.end(); }).listen(8080); //port //127.0.0.1:8080

JSON格式轉換為字串顯示
JSON.stringify({json格式})
檢查陣列是否從「字串」起始,是則true,否則false

  • 陣列.startWith('字串')
    將字串從以keyword為界,切割為陣列型態
  • 字串.split('要切割的keyword')
    刪除陣列的最後一個元素並刪除它
  • 陣列.pop()
    從陣列的指定位置刪除n個元素後增加元素
  • 陣列.splice(從第index個位置開始,刪除n個,新增什麼元素)

PATH Module

官網的PATH文件

  • 可以透過 var {變數名稱} = require('path'); 載入path模組
    • __dirname: 專案資料夾所在路徑
    • __filename: 檔案所在路徑
var path= require('path'); // 抓目錄路徑,輸出: /xx/yy console.log(path.dirname('/xx/yy/zz.js')); // 路徑合併,會從安裝槽(X:\)開始顯示路徑 console.log(path.join(__dirname,'/xx/yy/zz.js')); // 只顯示檔名,輸出: zz.js console.log(path.basename('/xx/yy/zz.js')); // 只顯示副檔名,輸出: .js console.log(path.extname('/xx/yy/zz.js')); // 分析路徑,以元件方式顯示路徑,輸出: // { root: '/', dir: '/xx/yy', base: 'zz.js', ext: '.js', name: 'zz' } console.log(path.parse('/xx/yy/zz.js'));

Todolist RESTful API kata

postman 下載
JSON Formatter:chrome格式化json檔的工具
JSONVue :功能同上

Universally Unique Identifier(UUID、通用唯一辨識碼)

類似身份證字號的東西,讓資訊具有唯一性
npm uuid install

// 呼叫uuid ,使用版本4 const {v4: uuidv4} =require('uuid'); // 顯示uuid const a = uuidv4(); console.log(a); // uuid也可以放在元件內 const obj={ 'title':'我是標題', 'uuid':uuidv4(), }

try catch

  • 類似if-else的東西,request的資料或物件沒有滿足try的格式則輸出catch內容
try{ 資料或物件格式、內容 }catch{ error ; }

preflight options API:跨網域預檢機制

  • method類型為option。指有跨網域的請求行為時,先行確認網域狀態再做第二次行為請求。

    大概就是第一次request會檢查來源是不是有鬼,第二次才會正式傳送資料。

heroku環境設置

const server = http.createServer(requestListener); // 監聽環境變數預設port 或 本地自訂port ,前者優先 server.listen(process.env.PORT || 8080);

修改package.json內容

"scripts":{ "start":"node server.js" } "engines":{ "node":"{node的版本號}" }

登入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