這里的 http 對象并不是 Node.js 里的 http 模塊,而是對 request 和 response 對象包裝后一個新的對象。
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ù)時,會傳遞 request 和 response 2個對象給回調(diào)函數(shù)。為了后續(xù)調(diào)用方便, ThinkJS 對這2個對象進行了包裝,包裝成了 http 對象,并且提供很多有用的方法。
http 對象會在 middleware, logic, controller, view 中傳遞。
注
:http 對象是 EventEmitter 的一個實例,所以可以對其進行事件監(jiān)聽和執(zhí)行。
系統(tǒng)原生的 request 對象
系統(tǒng)原生的 response 對象
請求的開始時間,是個unix
時間戳。
當前請求的 url 。
當前請求的 http 版本。
當前請求的類型。
當前請求的所有頭信息。
當前請求的 pathname,路由識別會依賴該值,會在后續(xù)的處理中對其進行改變。所以在 action 拿到值可能跟初始解析出來的值不一致。
當前請求的所有 query 數(shù)據(jù)。
當前請求的 host, 包含端口。
當前請求的 hostname,不包含端口。
當前請求的 payload 數(shù)據(jù),提交型的請求才含有該值。
表示當前請求的 payload 數(shù)據(jù)是否已經(jīng)解析。
存放 GET 參數(shù)值。
存放 POST 參數(shù)值
存放上傳的文件數(shù)據(jù)
存放 cookie 數(shù)據(jù)。
name
{String} 參數(shù)名return
{Mixed} 返回對應(yīng)的參數(shù)值獲取當前請求下對應(yīng)的參數(shù)值。
return
{String} 請求的 referrer返回當前請求的 referrer。
return
{String} 請求的 userAgent返回當前請求的 userAgent。
return
{Boolean}返回當前請求是否是 GET 請求。
return
{Boolean}返回當前請求是否是 POST 請求。
method
{String} 請求類型return
{Boolean}返回當前請求是否是 Ajax 請求。
http.isAjax(); //判斷是否是Ajax請求
http.isAjax("GET"); //判斷是否是Ajax請求,且請求類型是GET
name
{String} callback 參數(shù)名稱,默認為 callbackreturn
{Boolean}返回當前請求是否是 jsonp 請求。
//url is /index/test?callback=testxxx
http.isJsonp(); //true
http.isJsonp("cb"); //false
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"
name
{String} 參數(shù)名稱value
{Mixed} 參數(shù)值獲取或者設(shè)置 POST 值??梢酝ㄟ^該方法設(shè)置 POST 值,方便后續(xù)的邏輯里獲取。
http.post("email"); //獲取提交的email
name
{String} 參數(shù)名稱return
{Mixed}獲取參數(shù)值,優(yōu)先從 POST 里獲取,如果值為空,則從 URL 參數(shù)里獲取。
name
{String} 文件對應(yīng)的字段名稱return
{Object}獲取上傳的文件。
http.file("image");
//returns
{
fieldName: "image", //表單里的字段名
originalFilename: filename, //原始文件名
path: filepath, //文件臨時存放的路徑
size: size //文件大小
}
name
{String} header 名稱value
{String} header 值獲取或者設(shè)置 header 信息。
http.header("accept"); //獲取accept
http.header("X-NAME", "thinkjs"); //設(shè)置header
time
{Number} 過期時間,單位為秒強緩存,設(shè)置 Cache-Control
和 Expires
頭信息。
http.header(86400); //設(shè)置過期時間為 1 天。
設(shè)置狀態(tài)碼。如果頭信息已經(jīng)發(fā)送,則無法設(shè)置狀態(tài)碼。
http.status(400); //設(shè)置狀態(tài)碼為400
獲取用戶的 ip 。如果使用了代理,獲取的值可能不準。
lang
{String} 要設(shè)置的語言asViewPath
{Boolean} 是否添加一層模版語言目錄獲取或者設(shè)置國際化的語言,可以支持模版路徑要多一層語言的目錄。
let lang = http.lang();
獲取語言的循序為 http._lang
-> 從 cookie 中獲取
-> 從 header 中獲取
,如果需要從 url 中解析語言,可以獲取后通過 http.lang(lang)
方法設(shè)置到屬性 http._lang
中。
let lang = getFromUrl();
http.lang(lang, true); //設(shè)置語言,并指定模版路徑中添加一層語言目錄
獲取或者設(shè)置主題,設(shè)置后模版路徑要多一層主題的目錄。
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è)置無效
name
{String} session 名value
{Mixed} session 值return
{Promise}讀取、設(shè)置和清除 session。
let value = yield http.session("userInfo");
yield http.session("userInfo", data);
yield http.session();
url
{String} 要跳轉(zhuǎn)的 urlstatus
{Number} 狀態(tài)碼, 301 或者 302,默認為302頁面跳轉(zhuǎn)。
http.redirect("/login"); //跳轉(zhuǎn)到登錄頁面
contentType
{String} 要設(shè)置的 contentTypeencoding
{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
content
{Mixed} 要輸出的內(nèi)容encoding
{String} 編碼輸出內(nèi)容,要調(diào)用 http.end 才能結(jié)束當前請求。
content
{Mixed} 要輸出的內(nèi)容encoding
{String} 編碼輸出內(nèi)容并結(jié)束當前請求。
data
{Mixed} 要輸出的數(shù)據(jù)message
{String} 追加的message格式化輸出一個正常的數(shù)據(jù),一般是操作成功后輸出。
http.success({name: "thinkjs"});
//writes
{
errno: 0,
errmsg: "",
data: {
name: "thinkjs"
}
}
這樣客戶端就可以根據(jù)errno
是否為0
為判斷當前請求是否正常。
errno
{Number} 錯誤號errmsg
{String} 錯誤信息data
{Mixed} 額外的數(shù)據(jù)格式化輸出一個異常的數(shù)據(jù),一般是操作失敗后輸出。
注
:字段名errno
和errmsg
可以在配置里進行修改。
http.fail(100, "fail")
//writes
{
errno: 100,
errmsg: "fail",
data: ""
}
這樣客戶端就可以拿到具體的錯誤號和錯誤信息,然后根據(jù)需要顯示了。
注
:字段名errno
和errmsg
可以在配置里進行修改。
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
更多建議: