使用cookie
lor提供了一个内置的cookie插件,只要像require其它模块一样加载就可使用。
注意:
- 在使用这个插件时,用户应该先了解lua-resty-cookie的API使用方式
- 生产项目中如有其它cookie需求,用户应自行创建一个插件或是库来管理cookie, 建议参考lua-resty-cookie的实现
lor提供的默认cookie插件使用方式如下:
lua
local lor = require("lor.index")
local app = lor()
-- 加载cookie插件
local middleware_cookie = require("lor.lib.middleware.cookie")
-- 加载cookie插件后,`cookie`被注入到了`req`这个对象里
app:use(middleware_cookie())
-- 模拟cookie的写入
app:get("/set_cookie", function(req, res, next)
-- 使用res.cookie.set这个API来写cookie
req.cookie.set("test_cookie", "12345")
res:send("cookie was set.")
end)
-- 模拟cookie的读取, 返回`12345`
app:get("/set_cookie", function(req, res, next)
local test_cookie = req.cookie.get("test_cookie")
res:send("cookie is:".. test_cookie)
end)
app:run()