http(請求響應(yīng)對象)

2021-09-17 14:29 更新

這里的 http 對象并不是 Node.js 里的 http 模塊,而是對 request 和 response 對象包裝后一個(gè)新的對象。

var http = require("http");

http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
}).listen(8124);

如上面的代碼所示,Node.js 創(chuàng)建服務(wù)時(shí),會傳遞 request 和 response 2個(gè)對象給回調(diào)函數(shù)。為了后續(xù)調(diào)用方便, ThinkJS 對這2個(gè)對象進(jìn)行了包裝,包裝成了 http 對象,并且提供很多有用的方法。

http 對象會在 middleware, logic, controller, view 中傳遞。

:http 對象是 EventEmitter 的一個(gè)實(shí)例,所以可以對其進(jìn)行事件監(jiān)聽和執(zhí)行。

屬性

http.req

系統(tǒng)原生的 request 對象

http.res

系統(tǒng)原生的 response 對象

http.startTime

請求的開始時(shí)間,是個(gè)unix時(shí)間戳。

http.url

當(dāng)前請求的 url 。

http.version

當(dāng)前請求的 http 版本。

http.method

當(dāng)前請求的類型。

http.headers

當(dāng)前請求的所有頭信息。

http.pathname

當(dāng)前請求的 pathname,路由識別會依賴該值,會在后續(xù)的處理中對其進(jìn)行改變。所以在 action 拿到值可能跟初始解析出來的值不一致。

http.query

當(dāng)前請求的所有 query 數(shù)據(jù)。

http.host

當(dāng)前請求的 host, 包含端口。

http.hostname

當(dāng)前請求的 hostname,不包含端口。

http.payload

當(dāng)前請求的 payload 數(shù)據(jù),提交型的請求才含有該值。

http._payloadParsed

表示當(dāng)前請求的 payload 數(shù)據(jù)是否已經(jīng)解析。

http._get

存放 GET 參數(shù)值。

http._post

存放 POST 參數(shù)值

http._file

存放上傳的文件數(shù)據(jù)

http._cookie

存放 cookie 數(shù)據(jù)。

方法

http.config(name)

  • name {String} 參數(shù)名
  • return {Mixed} 返回對應(yīng)的參數(shù)值

獲取當(dāng)前請求下對應(yīng)的參數(shù)值。

http.referrer()

  • return {String} 請求的 referrer

返回當(dāng)前請求的 referrer。

http.userAgent()

  • return {String} 請求的 userAgent

返回當(dāng)前請求的 userAgent。

http.isGet()

  • return {Boolean}

返回當(dāng)前請求是否是 GET 請求。

http.isPost()

  • return {Boolean}

返回當(dāng)前請求是否是 POST 請求。

http.isAjax(method)

  • method {String} 請求類型
  • return {Boolean}

返回當(dāng)前請求是否是 Ajax 請求。

http.isAjax(); //判斷是否是Ajax請求
http.isAjax("GET"); //判斷是否是Ajax請求,且請求類型是GET

http.isJsonp(name)

  • name {String} callback 參數(shù)名稱,默認(rèn)為 callback
  • return {Boolean}

返回當(dāng)前請求是否是 jsonp 請求。

//url is  /index/test?callback=testxxx
http.isJsonp(); //true
http.isJsonp("cb"); //false

http.get(name, value)

  • name {String} 參數(shù)名稱
  • value {Mixed} 參數(shù)值

獲取或者設(shè)置 GET 參數(shù)值??梢酝ㄟ^該方法設(shè)置 GET 參數(shù)值,方便后續(xù)的邏輯里獲取。

// url is /index/test?name=thinkjs
http.get("name"); // returns "thinkjs"
http.get("name", "other value");
http.get("name"); // returns "other value"

http.post(name, value)

  • name {String} 參數(shù)名稱
  • value {Mixed} 參數(shù)值

獲取或者設(shè)置 POST 值??梢酝ㄟ^該方法設(shè)置 POST 值,方便后續(xù)的邏輯里獲取。

http.post("email"); //獲取提交的email

http.param(name)

  • name {String} 參數(shù)名稱
  • return {Mixed}

獲取參數(shù)值,優(yōu)先從 POST 里獲取,如果值為空,則從 URL 參數(shù)里獲取。

http.file(name)

  • name {String} 文件對應(yīng)的字段名稱
  • return {Object}

獲取上傳的文件。

http.file("image");
//returns 
{
  fieldName: "image", //表單里的字段名
  originalFilename: filename, //原始文件名
  path: filepath, //文件臨時(shí)存放的路徑
  size: size //文件大小
}

http.header(name, value)

  • name {String} header 名稱
  • value {String} header 值

獲取或者設(shè)置 header 信息。

http.header("accept"); //獲取accept
http.header("X-NAME", "thinkjs"); //設(shè)置header

http.expires(time)

  • time {Number} 過期時(shí)間,單位為秒

強(qiáng)緩存,設(shè)置 Cache-Control 和 Expires 頭信息。

http.header(86400); //設(shè)置過期時(shí)間為 1 天。

http.status(status)

設(shè)置狀態(tài)碼。如果頭信息已經(jīng)發(fā)送,則無法設(shè)置狀態(tài)碼。

http.status(400); //設(shè)置狀態(tài)碼為400

http.ip()

獲取用戶的 ip 。如果使用了代理,獲取的值可能不準(zhǔn)。

http.lang(lang, asViewPath)

  • lang {String} 要設(shè)置的語言
  • asViewPath {Boolean} 是否添加一層模版語言目錄

獲取或者設(shè)置國際化的語言,可以支持模版路徑要多一層語言的目錄。

獲取語言
let lang = http.lang();

獲取語言的循序?yàn)?nbsp;http._lang -> 從 cookie 中獲取 -> 從 header 中獲取,如果需要從 url 中解析語言,可以獲取后通過 http.lang(lang) 方法設(shè)置到屬性 http._lang 中。

設(shè)置語言
let lang = getFromUrl();
http.lang(lang, true); //設(shè)置語言,并指定模版路徑中添加一層語言目錄

http.theme(theme)

獲取或者設(shè)置主題,設(shè)置后模版路徑要多一層主題的目錄。

http.cookie(name, value)

  • name {String} cookie 名稱
  • value {String} cookie 值

讀取或者設(shè)置 cookie 值。

http.cookie("think_test"); //獲取名為 think_test 的 cookie
http.cookie("name", "value"); //設(shè)置 cookie,如果頭信息已經(jīng)發(fā)送則設(shè)置無效

http.session(name, value)

  • name {String} session 名
  • value {Mixed} session 值
  • return {Promise}

讀取、設(shè)置和清除 session。

讀取 Session
let value = yield http.session("userInfo");
設(shè)置 Session
yield http.session("userInfo", data);
清除 Session
yield http.session();

http.redirect(url, status)

  • url {String} 要跳轉(zhuǎn)的 url
  • status {Number} 狀態(tài)碼, 301 或者 302,默認(rèn)為302

頁面跳轉(zhuǎn)。

http.redirect("/login"); //跳轉(zhuǎn)到登錄頁面

http.type(contentType, encoding)

  • contentType {String} 要設(shè)置的 contentType
  • encoding {String} 要設(shè)置的編碼

獲取或者設(shè)置 Content-Type。

http.type(); //獲取Content-Type
http.type("text/html"); //設(shè)置Content-Type,會自動加上charset
http.type("audio/mpeg", false); //設(shè)置Content-Type,不追加charset

http.write(content, encoding)

  • content {Mixed} 要輸出的內(nèi)容
  • encoding {String} 編碼

輸出內(nèi)容,要調(diào)用 http.end 才能結(jié)束當(dāng)前請求。

http.end(content, encoding)

  • content {Mixed} 要輸出的內(nèi)容
  • encoding {String} 編碼

輸出內(nèi)容并結(jié)束當(dāng)前請求。

http.success(data, message)

  • data {Mixed} 要輸出的數(shù)據(jù)
  • message {String} 追加的message

格式化輸出一個(gè)正常的數(shù)據(jù),一般是操作成功后輸出。

http.success({name: "thinkjs"});
//writes
{
  errno: 0,
  errmsg: "",
  data: {
    name: "thinkjs"
  }
}

這樣客戶端就可以根據(jù)errno是否為0為判斷當(dāng)前請求是否正常。

http.fail(errno, errmsg, data)

  • errno {Number} 錯(cuò)誤號
  • errmsg {String} 錯(cuò)誤信息
  • data {Mixed} 額外的數(shù)據(jù)

格式化輸出一個(gè)異常的數(shù)據(jù),一般是操作失敗后輸出。

:字段名errnoerrmsg可以在配置里進(jìn)行修改。

http.fail(100, "fail")
//writes
{
  errno: 100,
  errmsg: "fail",
  data: ""
}

這樣客戶端就可以拿到具體的錯(cuò)誤號和錯(cuò)誤信息,然后根據(jù)需要顯示了。

:字段名errnoerrmsg可以在配置里進(jìn)行修改。

http.json(data)

  • data {Object}

json 方式輸出數(shù)據(jù),會設(shè)置 Content-Type 為 application/json,該值對應(yīng)的配置為json_content_type

文檔地址:https://github.com/75team/www.thinkjs.org/tree/master/view/zh-CN/doc/2.0/api_http.md

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號