Luvit像Node.js一样写Lua应用

luvit是什么?它是目前Lua这个小众语言中较为流行的一个开源框架,给那些习惯使用Lua的开发者一个机会向写Node.js一样用Lua进行开发,它是Lua的Node.js.在Gitlab上,项目的描述对于Luvit是这样描述的:Lua + libUV + jIT = pure awesomesauce。

概要信息

安装

提供了安装脚本进行一键安装,但是版本不是最新,如果需要最新的版本,可以从源码开始

curl -L https://github.com/luvit/lit/raw/master/get-lit.sh | sh

安装日志

[root@kong ~]# curl -L https://github.com/luvit/lit/raw/master/get-lit.sh | sh
 % Total  % Received % Xferd Average Speed  Time  Time   Time Current
                 Dload Upload  Total  Spent  Left Speed
100  127 100  127  0   0   57   0 0:00:02 0:00:02 --:--:--  57
100  567 100  567  0   0  192   0 0:00:02 0:00:02 --:--:-- 1788
Downloading https://github.com/luvit/luvi/releases/download/v2.7.6/luvi-regular-Linux_x86_64 to luvi
 % Total  % Received % Xferd Average Speed  Time  Time   Time Current
                 Dload Upload  Total  Spent  Left Speed
100  615  0  615  0   0  280   0 --:--:-- 0:00:02 --:--:--  280
100 4261k 100 4261k  0   0  481k   0 0:00:08 0:00:08 --:--:-- 1007k
Downloading https://lit.luvit.io/packages/luvit/lit/v3.5.4.zip to lit.zip
 % Total  % Received % Xferd Average Speed  Time  Time   Time Current
                 Dload Upload  Total  Spent  Left Speed
100 234k 100 234k  0   0 91459   0 0:00:02 0:00:02 --:--:-- 91488
lit version: 3.5.4
luvi version: v2.7.6
command: make lit.zip lit luvi
create config: /root/.litconfig
compiling filter: ** excludes by default (first rule is positive)
including: commands/README
including: commands/add.lua
including: commands/auth.lua
including: commands/claim.lua
including: commands/config.lua
including: commands/down.lua
including: commands/get-luvi.lua
including: commands/help.lua
including: commands/init.lua
including: commands/install.lua
including: commands/ls.lua
including: commands/make.lua
including: commands/publish.lua
including: commands/serve.lua
including: commands/share.lua
including: commands/sync.lua
including: commands/unclaim.lua
including: commands/up.lua
including: commands/update.lua
including: init.lua
including: libs/api.lua
including: libs/auto-updater.lua
including: libs/autoconfig.lua
including: libs/calculate-deps.lua
including: libs/codec.lua
including: libs/core.lua
including: libs/db-fs.lua
including: libs/db.lua
including: libs/exec.lua
including: libs/export-zip.lua
including: libs/export.lua
including: libs/get-installed.lua
including: libs/github-request.lua
including: libs/handlers.lua
including: libs/import.lua
including: libs/install-deps.lua
including: libs/log.lua
including: libs/metrics.lua
including: libs/pkg.lua
including: libs/rdb.lua
including: libs/rules.lua
including: libs/snapshots.lua
including: libs/stats.lua
including: libs/verify-signature.lua
including: libs/vfs.lua
including: luvit-loader.lua
including: main.lua
including: package.lua
including dependency: base64 (deps/base64.lua)
including dependency: coro-channel (deps/coro-channel.lua)
including dependency: coro-fs (deps/coro-fs.lua)
including dependency: coro-http (deps/coro-http.lua)
including dependency: coro-net (deps/coro-net.lua)
including dependency: coro-spawn (deps/coro-spawn.lua)
including dependency: coro-split (deps/coro-split.lua)
including dependency: coro-websocket (deps/coro-websocket.lua)
including dependency: coro-wrapper (deps/coro-wrapper.lua)
including dependency: git (deps/git/)
including dependency: http-codec (deps/http-codec.lua)
including dependency: json (deps/json.lua)
including dependency: md5 (deps/md5.lua)
including dependency: pathjoin (deps/pathjoin.lua)
including dependency: pretty-print (deps/pretty-print.lua)
including dependency: prompt (deps/prompt.lua)
including dependency: querystring (deps/querystring.lua)
including dependency: readline (deps/readline.lua)
including dependency: resource (deps/resource.lua)
including dependency: secure-socket (deps/secure-socket/)
including dependency: semver (deps/semver.lua)
including dependency: sha1 (deps/sha1.lua)
including dependency: ssh-rsa (deps/ssh-rsa.lua)
including dependency: ustring (deps/ustring.lua)
including dependency: weblit-app (deps/weblit-app.lua)
including dependency: weblit-auto-headers (deps/weblit-auto-headers.lua)
including dependency: weblit-websocket (deps/weblit-websocket.lua)
including dependency: websocket-codec (deps/websocket-codec.lua)
compiling filter: deps/git/** excludes by default (first rule is positive)
including: core.lua
including: db.lua
including: init.lua
including: package.lua
including: storage.lua
creating binary: /root/lit
using luvi from: /root/luvi
inserting luvi: /root/luvi
storing: commands/add.lua
compiling: commands/auth.lua (9.9% reduction)
storing: commands/claim.lua
storing: commands/config.lua
storing: commands/down.lua
compiling: commands/get-luvi.lua (20.7% reduction)
storing: commands/help.lua
compiling: commands/init.lua (14.9% reduction)
storing: commands/install.lua
compiling: commands/ls.lua (2.6% reduction)
storing: commands/make.lua
storing: commands/publish.lua
compiling: commands/serve.lua (19% reduction)
storing: commands/share.lua
storing: commands/sync.lua
storing: commands/unclaim.lua
storing: commands/up.lua
compiling: commands/update.lua (23.3% reduction)
compiling: deps/base64.lua (29.2% reduction)
compiling: deps/coro-channel.lua (19.7% reduction)
storing: deps/coro-fs.lua
compiling: deps/coro-http.lua (30.4% reduction)
compiling: deps/coro-net.lua (18.5% reduction)
compiling: deps/coro-spawn.lua (44% reduction)
compiling: deps/coro-split.lua (34.4% reduction)
compiling: deps/coro-websocket.lua (34.6% reduction)
compiling: deps/coro-wrapper.lua (66.1% reduction)
compiling: deps/git/core.lua (3.4% reduction)
compiling: deps/git/db.lua (18.7% reduction)
storing: deps/git/init.lua
storing: deps/git/package.lua
compiling: deps/git/storage.lua (34.5% reduction)
compiling: deps/http-codec.lua (37.2% reduction)
compiling: deps/json.lua (30.7% reduction)
storing: deps/md5.lua
compiling: deps/pathjoin.lua (33.5% reduction)
compiling: deps/pretty-print.lua (30.4% reduction)
compiling: deps/prompt.lua (43.4% reduction)
compiling: deps/querystring.lua (35.9% reduction)
compiling: deps/readline.lua (18.6% reduction)
compiling: deps/resource.lua (31.5% reduction)
compiling: deps/secure-socket/biowrap.lua (31.8% reduction)
compiling: deps/secure-socket/context.lua (27.4% reduction)
compiling: deps/secure-socket/init.lua (52.5% reduction)
storing: deps/secure-socket/package.lua
compiling: deps/semver.lua (28.2% reduction)
compiling: deps/sha1.lua (27.9% reduction)
compiling: deps/ssh-rsa.lua (16.2% reduction)
compiling: deps/ustring.lua (37.1% reduction)
compiling: deps/weblit-app.lua (14.8% reduction)
compiling: deps/weblit-auto-headers.lua (50.9% reduction)
compiling: deps/weblit-websocket.lua (35.8% reduction)
compiling: deps/websocket-codec.lua (18.2% reduction)
storing: init.lua
compiling: libs/api.lua (30.9% reduction)
compiling: libs/auto-updater.lua (29% reduction)
compiling: libs/autoconfig.lua (32.3% reduction)
compiling: libs/calculate-deps.lua (30.4% reduction)
compiling: libs/codec.lua (31.2% reduction)
compiling: libs/core.lua (14% reduction)
compiling: libs/db-fs.lua (29.5% reduction)
compiling: libs/db.lua (38.2% reduction)
compiling: libs/exec.lua (45.1% reduction)
compiling: libs/export-zip.lua (36.4% reduction)
compiling: libs/export.lua (36.6% reduction)
compiling: libs/get-installed.lua (45.9% reduction)
compiling: libs/github-request.lua (49.8% reduction)
compiling: libs/handlers.lua (18.1% reduction)
compiling: libs/import.lua (27.6% reduction)
compiling: libs/install-deps.lua (38.4% reduction)
compiling: libs/log.lua (60.7% reduction)
compiling: libs/metrics.lua (56.8% reduction)
compiling: libs/pkg.lua (36.9% reduction)
compiling: libs/rdb.lua (22.2% reduction)
compiling: libs/rules.lua (29.7% reduction)
compiling: libs/snapshots.lua (12.6% reduction)
compiling: libs/stats.lua (1.5% reduction)
compiling: libs/verify-signature.lua (40.8% reduction)
compiling: libs/vfs.lua (29.9% reduction)
compiling: luvit-loader.lua (29.8% reduction)
compiling: main.lua (24.3% reduction)
compiling: package.lua (14.8% reduction)
done building: /root/lit
done: success
lit version: 3.5.4
luvi version: v2.7.6
command: make lit://luvit/luvit luvit luvi
load config: /root/.litconfig
connecting: wss://lit.luvit.io/
fetching: 1 object
fetching: 1 object
fetching: 4 objects
using snapshot: ab0b6ace5a16a88e6fbb4c8da0083c40cf1d6803
creating binary: /root/luvit
using luvi from: /root/luvi
fetching: 1 object
fetching: 1 object
fetching: 27 objects
fetching: 16 objects
inserting luvi: /root/luvi
compiling: deps/buffer.lua (16.5% reduction)
compiling: deps/childprocess.lua (19.1% reduction)
compiling: deps/codec.lua (35.3% reduction)
compiling: deps/core.lua (57.3% reduction)
compiling: deps/dgram.lua (25.5% reduction)
compiling: deps/dns.lua (25.1% reduction)
compiling: deps/fs.lua (6.2% reduction)
compiling: deps/helpful.lua (42.7% reduction)
compiling: deps/hooks.lua (83.3% reduction)
compiling: deps/http-codec.lua (37.4% reduction)
compiling: deps/http-header.lua (59.4% reduction)
compiling: deps/http.lua (29.6% reduction)
compiling: deps/https.lua (45.6% reduction)
compiling: deps/json.lua (30.7% reduction)
compiling: deps/los.lua (76.9% reduction)
compiling: deps/net.lua (13.9% reduction)
compiling: deps/path/base.lua (24.9% reduction)
compiling: deps/path/init.lua (49.7% reduction)
storing: deps/path/package.lua
compiling: deps/pathjoin.lua (33.5% reduction)
compiling: deps/pretty-print.lua (30.6% reduction)
compiling: deps/process.lua (17.2% reduction)
compiling: deps/querystring.lua (35.9% reduction)
compiling: deps/readline.lua (18.6% reduction)
compiling: deps/repl.lua (28.2% reduction)
compiling: deps/require.lua (24.6% reduction)
compiling: deps/resource.lua (31.5% reduction)
compiling: deps/stream/init.lua (55.6% reduction)
storing: deps/stream/package.lua
compiling: deps/stream/stream_core.lua (38.5% reduction)
compiling: deps/stream/stream_duplex.lua (49.7% reduction)
compiling: deps/stream/stream_observable.lua (50.8% reduction)
compiling: deps/stream/stream_passthrough.lua (65.3% reduction)
compiling: deps/stream/stream_readable.lua (48.2% reduction)
compiling: deps/stream/stream_transform.lua (62% reduction)
compiling: deps/stream/stream_writable.lua (43% reduction)
compiling: deps/thread.lua (38.6% reduction)
compiling: deps/timer.lua (24% reduction)
compiling: deps/tls/common.lua (14.9% reduction)
compiling: deps/tls/init.lua (26.1% reduction)
compiling: deps/tls/lcrypto.lua (46.8% reduction)
compiling: deps/tls/package.lua (2.6% reduction)
compiling: deps/url.lua (32.8% reduction)
compiling: deps/ustring.lua (37.5% reduction)
compiling: deps/utils.lua (39.7% reduction)
compiling: init.lua (43.5% reduction)
compiling: main.lua (21.6% reduction)
compiling: package.lua (21.1% reduction)
storing: test.lua
done building: /root/luvit
done: success
[root@kong ~]#

安装结果确认

[root@kong ~]# ./lit --version
lit version: 3.5.4
luvi version: v2.7.6
[root@kong ~]# ./luvi --version
./luvi v2.7.6
rex: 8.37 2015-04-28
libuv: 1.9.1
ssl: OpenSSL 1.0.2h 3 May 2016, lua-openssl 0.5.1
[root@kong ~]# ./luvit --version
luvit version: 2.14.2
luvi version: v2.7.6
rex version: 8.37 2015-04-28
libuv version: 1.9.1
ssl version: OpenSSL 1.0.2h 3 May 2016, lua-openssl 0.5.1
[root@kong ~]#

拷贝至PATH搜索目录

[root@kong ~]# cp lit /usr/local/bin
[root@kong ~]# cp luvi /usr/local/bin
[root@kong ~]# cp luvit /usr/local/bin
[root@kong ~]#
[root@kong ~]# which luvi
/usr/local/bin/luvi
[root@kong ~]# which lit
/usr/local/bin/lit
[root@kong ~]# which luvit
/usr/local/bin/luvit
[root@kong ~]#

Lua界的Node.js

一个Node.js的HelloWorld的server.js可能是这样的,运行则是使用node server.js

var http = require('http');
http.createServer(function (req, res) {
 var body = 'Hello world\n';
 res.setHeader('Content-Type', 'text/plain');
 res.setHeader('Content-Length', body.length);
 res.end(body);
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

而Luvit的HelloWorld的Server.lua则是这样的,运行则是使用luvit server.lua

[root@kong ~]# cat server.lua
local http = require('http')
http.createServer(function (req, res)
 local body = 'Hello world\n'
 res:setHeader('Content-Type', 'text/plain')
 res:setHeader("Content-Length", #body)
 res:finish(body)
end):listen(1337, '127.0.0.1')
print('Server running at http://127.0.0.1:1337/')
[root@kong ~]#

运行结果为:

[root@kong ~]# luvit server.lua
Server running at http://127.0.0.1:1337/

确认结果:

[root@kong ~]# curl http://localhost:1337
Hello world
[root@kong ~]#

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接

(0)

相关推荐

  • Lua在各个操作系统中的开发环境配置教程

     Lua开发环境设置 如果愿意设置您的Lua编程语言环境中,需要用计算机上的以下两个软件,(a)文字编辑器,(b)Lua解释,以及(c)Lua编译器. 文本编辑器 这将被用来输入编写程序.一些编辑器包括Windows记事本,操作系统Edit命令,Brief,Epsilon,Emacs和VIM或VI. 文本编辑器名称和版本可以在不同的操作系统上.例如,记事本可用Windows上,vim或者vi可以在Windows以及Linux或UNIX上使用. 编辑器创建文件称为源文件和包含程序的源代码.在Lua

  • Lua中三种循环语句的使用讲解

    Lua的循环和C语言的循环的语法其实差不多,所以,理解起来就很好理解的啦,所以实现也很简单,跟C没什么两样,都差不多. 案例如下: test1.lua -- 1.while循环 --[[ 理解为C语言的就行了,其实差不多的 语法格式: while(true) do 执行语句 end ]] --定义一个全局变量a=0 a=0 -- while(true) do a=a+1 print("a:",a) if(a == 5) then break end end -- 2.for循环 --[

  • Lua中的变量与赋值方法

    看以下案例: test.lua -- 第一个lua脚本 --注释使用"--"符 --变量未定义时,默认初始化的值为nil --这样的定义为全局 num1 = 1 ; --加了关键字local表示这个变量是局部变量 local num2 = 2 ; --定义变量的末尾不加分号;也是可以的,个人建议,因为Lua是C写的,写分号还是规范点 num3 = 3 --定义一个函数,目的是实现两数相加并返回 function add() --a = 1 也可以在函数内部定义 --b = 2 retu

  • lua开发中实现MVC框架的简单应用

    先简单说说MVC,即Model View Controller.Model(模型),一般负责数据的处理:View(视图),一般负责界面的显示:Controller(控制器),一般负责前端的逻辑处理.拿一款手机游戏来说,界面UI的显示.布局等就是View负责:点击了按钮,手势的滑动等操作由Controller来处理:游戏中需要的数据资源就交给Model. 接下来,看看在游戏开发中怎么用,这里用Lua(环境使用cocos code ide)给大家说说. 先来看看项目的目录结构: 其中cocos.Co

  • Nginx安装lua-nginx-module模块的方法步骤

    ngx_lua_module 是一个nginx http模块,它把 lua 解析器内嵌到 nginx,用来解析并执行lua 语言编写的网页后台脚本 特性很牛叉,可自行百度查看,这里主要是示范一下,如何在Nginx下安装lua-nginx-module模块 当然,如果你之前没有安装过Nginx,而且嫌安装麻烦,可直接下载openresty安装简单快捷,http://openresty.org/cn/installation.html(阿里的大牛章亦春的作品,膜拜~~~) 1.下载安装LuaJIT

  • cocos2dx+lua实现橡皮擦功能

    游戏中刮刮乐是怎么实现的?做了一个小例子看了一下. 实现原理:随着触摸点的移动,通过setBlendFunc函数设置部分区域的颜色混合(将上层图片透明度为0,底层我们想要的图片就显示出来) --橡皮擦功能测试 local function initInfo() local scene = CCScene:create() local layer = CCLayer:create() scene:addChild(layer) --擦除后要显示的图片 local tupian = CCSprite

  • Centos7 安装Nginx整合Lua的示例代码

    前言 本人的使用的电脑是Mac,操作系统是macOS Mojave.电脑上装有虚拟机. 虚拟机上安装Centos7操作系统,在其之上安装Nginx及Luau类库,整个过程是在系统安装完成之后开始记录. 建议安装前先拍快照,出现问题可以恢复 准备工作 如果安装的Linux能够联网,并且外部也能正常使用Linux的端口,那么可以忽略下面两部 1.设置自动获取ip (1)在Linux上输入命令 [root@localhost ~]ip addr #查看ip [root@localhost ~]nmcl

  • Lua协同程序coroutine的简介及优缺点

    什么是协同(coroutine)? Lua 协同程序(coroutine)与线程比较类似:拥有独立的堆栈,独立的局部变量,独立的指令指针,同时又与其它协同程序共享全局变量和其它大部分东西. 协同是非常强大的功能,但是用起来也很复杂. 线程和协同程序区别 协程是编译器级别的,线程是操作系统级别的,在多处理器情况下,多线程程序同时运行多个线程:而协同程序是通过协作来完成,在任一指定时刻只有一个协同程序在运行,并且这个正在运行的协同程序只在必要时才会被挂起.这样Lua的协程就不能利用现在多核技术了.

  • Lua游戏开发教程之时区问题详解

    前言 什么是Lua? Lua 是一个小巧的脚本语言,巴西里约热内卢天主教大学里的一个研究小组于1993年开发,其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能.Lua由标准C编写而成,几乎在所有操作系统和平台上都可以编译,运行.一个完整的Lua解释器不过200k,在目前所有脚本引擎中,Lua的速度是最快的.这一切都决定了Lua是作为嵌入式脚本的最佳选择.相比Python和Per的内核,Lua的内核小于120KB,而Python的内核大约860KB,Perl的内核大约1.1M

  • 安装Nginx+Lua开发环境

    首先我们选择使用OpenResty,其是由Nginx核心加很多第三方模块组成,其最大的亮点是默认集成了Lua开发环境,使得Nginx可以作为一个Web Server使用.借助于Nginx的事件驱动模型和非阻塞IO,可以实现高性能的Web应用程序.而且OpenResty提供了大量组件如Mysql.Redis.Memcached等等,使在Nginx上开发Web应用更方便更简单.目前在京东如实时价格.秒杀.动态服务.单品页.列表页等都在使用Nginx+Lua架构,其他公司如淘宝.去哪儿网等. 安装环境

随机推荐