nodejs的express框架(request,response方法彙總)

Response 對象 - response 對象表示 HTTP 響應,即在接收到請求時向客戶端發送的 HTTP 響應數據。常見屬性有:express

  1. res.app:同req.app同樣
  2. res.append():追加指定HTTP頭
  3. res.set()在res.append()後將重置以前設置的頭
  4. res.cookie(name,value [,option]):設置Cookie
  5. opition: domain / expires / httpOnly / maxAge / path / secure / signed
  6. res.clearCookie():清除Cookie
  7. res.download():傳送指定路徑的文件
  8. res.get():返回指定的HTTP頭
  9. res.json():傳送JSON響應
  10. res.jsonp():傳送JSONP響應
  11. res.location():只設置響應的Location HTTP頭,不設置狀態碼或者close response
  12. res.redirect():設置響應的Location HTTP頭,而且設置狀態碼302
  13. res.send():傳送HTTP響應
  14. res.sendFile(path [,options] [,fn]):傳送指定路徑的文件 -會自動根據文件extension設定Content-Type
  15. res.set():設置HTTP頭,傳入object能夠一次設置多個頭
  16. res.status():設置HTTP狀態碼
  17. res.type():設置Content-Type的MIME類型

Request 對象 - request 對象表示 HTTP 請求,包含了請求查詢字符串,參數,內容,HTTP 頭部等屬性。常見屬性有:json

  1. req.app:當callback爲外部文件時,用req.app訪問express的實例
  2. req.baseUrl:獲取路由當前安裝的URL路徑
  3. req.body / req.cookies:得到「請求主體」/ Cookies
  4. req.fresh / req.stale:判斷請求是否還「新鮮」
  5. req.hostname / req.ip:獲取主機名和IP地址
  6. req.originalUrl:獲取原始請求URL
  7. req.params:獲取路由的parameters
  8. req.path:獲取請求路徑
  9. req.protocol:獲取協議類型
  10. req.query:獲取URL的查詢參數串
  11. req.route:獲取當前匹配的路由
  12. req.subdomains:獲取子域名
  13. req.accepts():檢查可接受的請求的文檔類型
  14. req.acceptsCharsets / req.acceptsEncodings / req.acceptsLanguages:返回指定字符集的第一個可接受字符編碼
  15. req.get():獲取指定的HTTP請求頭
  16. req.is():判斷請求頭Content-Type的MIME類型
相關文章
相關標籤/搜索