Getting data from request(req)

Ryan Lai
2 min readJun 12, 2021

Also on Strikingly:
https://site-4828247-981-6937.mystrikingly.com/blog/getting-data-from-request-req

更新

新增 URL (取得 “網址” 內容):2-3. req.method (取得 HTTP Method)

前言

Express-Request

The req object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on. In this documentation and by convention, the object is always referred to as req (and the HTTP response is res) but its actual name is determined by the parameters to the callback function in which you’re working.

根據常用 req 內容,依來源位置,歸納為 4 類(共5種):

  1. HTML Form (取得 “表單” 內容):req.body
  2. URL (取得 “網址” 內容):
    2-1. req.query、2-2. req.params、2–3. req.method (取得 HTTP Method)
  3. Passport 套件+資料關聯:req.user
  4. Flash Message (提示 User):req.flash(“success_messages”)、req.flash(“error_messages”)
  1. HTML Form:req.body (body-parser 取得 Form 內容)

2–1. URL (取得 “網址” 內容):req.query (取得 URL 中,?後的內容)

2–2. URL (取得 “網址” 內容):req.params (取得 動態路由 [:params ])

2–3. req.method

3. Passport 套件+資料關聯:req.user (取得 User 資料)

4. Flash Message (提示 User):
req.flash(“success_messages”)、req.flash(“error_messages”)

(1) req.flash 傳遞流程

(2) req.flash 設定方式

(3) 模組化 message 樣板

--

--

Ryan Lai

Equipped with web development, communication, and business analytical skills.