HTTP

2019-04-02 11:43 更新

介紹

http庫無需初始化, 導入后直接使用.

http.ok()

此方法在使用httpd對象注冊了before處理方法之后, 可以直接return http.ok().

當before函數(shù)正確檢測到返回http.ok()后, 允許當前路由繼續(xù)執(zhí)行到用戶回調(diào)函數(shù)(類).

http.redirect(url, code)

此方法在使用httpd對象注冊了before處理方法之后, 可以直接return http.redirect(your url).

當before函數(shù)正確檢測到返回http.redirect()后, 將會任務將請求重定向到其它http[s]鏈接上.

第二個參數(shù)code為可選的http跳轉(zhuǎn)碼, 只能是301302(不傳入code默認情況下是302);

http.throw(code, html)

此方法在使用httpd對象注冊了before處理方法之后, 可以直接return http.throw(code, html).

當before函數(shù)正確檢測到返回http.throw()后, 將會使用指定的code狀態(tài)碼來進行返回(僅允許400-499之間的錯誤碼);

第二個參數(shù)為可選的html代碼, 作為自定義錯誤碼的內(nèi)容(可以在調(diào)試階段將錯誤日志打印出來).

使用示例

app:before(function (content)
    if true then
        return http.ok()
    end
    if true then
        return http.redirect('https://github.com/CandyMi/core_framework')
        -- return http.redirect('https://github.com/CandyMi/core_framework', 301 or 302)
    end
    if true then
        return http.throw(431, '<h1> This is 413 Error, too long request header</h1>')
    end
end)
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號