OpenResty 简单编写一个Module,openrestymodule


使用 Lua module 来进行 Lua 代码的复用是推荐的做法。然后在用户代码中直接用require()来调用

module代码:

local myTest = {}

function myTest:getid()
   local str = "123"
   return str
end

return myTest

开头有这样一行代码module(...),这是为了模块名以文件名命名

将文件放到“/usr/local/openresty/lualib/lib”中(或设置lua_package_path)

调用:

local test = require "lib.myTest"
local str = test:getid()
ngx.say(str)

 

相关内容