码迷,mamicode.com
首页 > 其他好文 > 详细

OpenResty 简单编写一个Module

时间:2014-11-01 14:40:13      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   使用   sp   文件   

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

module代码:

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)

 

OpenResty 简单编写一个Module

标签:style   blog   http   io   color   ar   使用   sp   文件   

原文地址:http://www.cnblogs.com/kgdxpr/p/4067060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!