阅读(1889) (0)

HTTP

2019-03-30 18:32:58 更新

介绍

http库无需初始化, 导入后直接使用.

http.ok()

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

当before函数正确检测到返回http.ok()后, 允许当前路由继续执行到用户回调函数(类).

http.redirect(url, code)

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

当before函数正确检测到返回http.redirect()后, 将会任务将请求重定向到其它http[s]链接上.

第二个参数code为可选的http跳转码, 只能是301302(不传入code默认情况下是302);

http.throw(code, html)

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

当before函数正确检测到返回http.throw()后, 将会使用指定的code状态码来进行返回(仅允许400-499之间的错误码);

第二个参数为可选的html代码, 作为自定义错误码的内容(可以在调试阶段将错误日志打印出来).

使用示例

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)