lapis使用

编程入门 行业动态 更新时间:2024-10-28 04:24:08

<a href=https://www.elefans.com/category/jswz/34/1693045.html style=lapis使用"/>

lapis使用

lapis:

安装

/ 官网安装方式依赖lua-cjson,但是已经安装了openresty的可能会报错。
解决方法,使用下面issue中的luarocks install lua-cjson2

使用

lapis提供lua和moonscript两种代码。

生成一个新的项目

默认生成moonscript脚本,增加--lua这个flag可以生成lua的脚本。包含框架的四个基本的文件。
lapis new --lua 

运行

如果安装了openresty,使用

lapis server   

打开http://localhost:8080可以看到欢迎界面。

配置,在当前文件夹创建一个文件config.lua

local config = require("lapis.config")config("development", {port = 9090
})

启动之后就端口就改成了9090端口。

视图

创建一个views的文件夹,里面放一个文件index.etlua,内容如下:

<h1>Hello world</h1>
<p>Welcome to my page</p>

lapis将会将index.etlua文件内容解析输出为html网页格式。需要使用app:enable("etlua")来让lapis解析对应的etlua文件为html。

简单封装使用

local respond_to = require ("lapis.application").respond_to
app:enable("etlua")local blm_hc_status = require "controllers.blm_hc_status"
app:match("/hello", respond_to(blm_hc_status))

新建的文件夹controllers文件夹下面,创建一个文件,因为是match,里面可以写四种方法,访问http://localhost:9090/hello

local db = require("lapis.db")local mt = {}
function mt:GET()local res = db.query("select * from tb_hc_status")self.services = resreturn { render = "index" }
endreturn mt

转载于:.html

更多推荐

lapis使用

本文发布于:2024-02-17 06:37:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1693039.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:lapis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!