Asynchronous I/O for Lua
InstallFamiliar API
Luvit implements the same APIs as Node.js, but in Lua!
This helps teams migrate without having to learn a new way of programming.
Async Choice
Choose your async model; we don’t mind; we encourage experimentation.
If you don’t like callbacks and event emitters, use coroutines and write blocking style code without actually blocking your event loop!
Modular Core
The various projects in the luvit ecosystem can be mixed and matched to build the ideal runtime for your application.
- Use luv directly in luajit
- Use lit without node apis
- The possibilities are endless
Using the Node-Style APIs
The luvit
CLI tool can be used as a scripting platform just like node
. This
can be used to run lua scripts as standalone servers, clients, or other tools.
This simple web server written in Luvit responds with Hello World
for every
request.
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/')
And run this script using luvit
.
> luvit server.lua
Server running at http://127.0.0.1:1337/
This script is a standalone HTTP server, there is no need for Apache or Nginx to act as host.
Using Third-Party Libraries
Luvit also has a package system that makes it easy to publish and consume libraries.
For example, @creationix has made a set of libraries that use coroutines instead of callbacks for async I/O and published these to lit.
Using lit
install creationix/weblit
to use an express-like framework built
on top of coroutines.
> mkdir myapp && cd myapp
> lit install creationix/weblit
> vim server.lua
> luvit server.lua
The server.lua
file will contain:
local weblit = require('weblit')
weblit.app
.bind({host = "127.0.0.1", port = 1337})
-- Configure weblit server
.use(weblit.logger)
.use(weblit.autoHeaders)
-- A custom route that sends back method and part of url.
.route({ path = "/:name"}, function (req, res)
res.body = req.method .. " - " .. req.params.name .. "\n"
res.code = 200
res.headers["Content-Type"] = "text/plain"
end)
-- Start the server
.start()
This very site is being served by weblit
and its source can be found at
https://github.com/luvit/luvit.io
Permissive License
Luvit is licensed under the Apache 2.0 License to The Luvit Authors
. This
was done to make the project as accessible as possible to users and
contributors.
Dive In
Join us on freenode IRC at #luvit, the Luvit Mailing list, or the Discord Server
We’ll be publishing tutorials here at the luvit blog soon, stay tuned.