Display Hello Algernon! in your browser.
Create a hello.lua file that looks like this:
handle("/", function()
content("text/plain")
print("Hello Algernon!")
end)handleis a built-in function that can serve an endpoint with a given function.function() [...] endis an anonymous Lua function.contentis a built-in function for settting the content type / MIME type, such astext/plain,text/htmlorimage/png.printis a built-in function for outputting text to the HTTP client (typically a browser) that is visiting this page.
Run algernon hello.lua
Visit http://localhost:3000 in your favorite browser.
Use a directory structure instead of a single Lua file with handlers.
mkdir simple
cd simpleCreate an index.lua file that looks like this:
print("the light")Serve the current directory with Algernon:
algernon .Visit http://localhost:3000 in a browser and see "the light".
Serve an image by printing it from Lua, and then use it within an HTML page.
mkdir eyes
cd eyesCreate an eye.lua file that looks like this:
content("image/svg+xml")
print [[
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40" fill="white" stroke="black" stroke-width="2" />
<circle cx="38" cy="50" r="20" fill="black" />
<circle cx="28" cy="42" r="5" fill="white" />
</svg>
]]This outputs an SVG image.
Create an index.lua file that looks like this:
content("text/html")
print [[
<!doctype html><html><body>
<img src="eye.lua" width="25em">
<img src="eye.lua" width="25em">
]]This is the main/default handler for this directory, and outputs a simple HTML page that displays two images.
The browser does not care if the images end with an unusual file extension such as .lua, because the content-type / MIME type regulates this.
algernon -e .Note that the -e flag is for "development mode", where error messages may appear directly in the browser, and pages are not cached.
- Visit
http://localhost:3000/eye.luain a browser and observe the result of serving an SVG image. - Visit
http://localhost:3000/in a browser and observe the result of serving the HTML document (which useseye.lua, twice).
- In ie. Firefox, press
F12(orfn+F12on macOS). Select theNetworktab and then reload the page. - Click the
200 | GET | localhost:3000 | eye.lua | ...row. - On the right hand side, observe that
Content-Typeisimage/svg+xmland that theServerisAlgernonand a version number. - There are also various security-related headers that have been set (that can be turned off with the
--no-headersflag).
Style a HTML page with CSS and try out the auto-refresh feature.
mkdir style
cd styleCreate an eye.svg file that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40" fill="white" stroke="black" stroke-width="4" />
<circle cx="38" cy="50" r="20" fill="black" />
<circle cx="28" cy="42" r="5" fill="white" />
</svg>Create a mouth.svg file that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="50" viewBox="0 0 100 50">
<path d="M 10,25 Q 50,50 90,25 Q 50,0 10,25 Z" fill="white" stroke="black" stroke-width="2"/>
</svg>Create an index.html file that looks like this:
<!doctype html>
<html>
<head>
<title>Face</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="face">
<img src="eye.svg" id="left_eye">
<img src="eye.svg" id="right_eye">
<img src="mouth.svg" id="mouth">
</div>
</body>
</html>
Create a style.css file that looks like this:
#face {
position: relative;
top: 4em;
left: 4em;
width: 12.5em;
height: 12.5em;
background: lightblue;
border-radius: 1.25em;
}
#left_eye, #right_eye, #mouth {
position: absolute;
}
#left_eye, #right_eye {
animation: rotate 16s linear infinite;
}
#left_eye {
top: 2em;
left: 0.75em;
width: 5.625em;
}
#right_eye {
top: 2em;
left: 6.125em;
width: 5.625em;
}
#mouth {
top: 7em;
left: 3.125em;
width: 6.25em;
}
@keyframes rotate {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
Serve the page with:
algernon -a -e --no-headers .- Note that serving with
algernon -a -eand visitinghttp://127.0.0.1:3000instead of using--no-headersand visitinghttp://localhost:3000also works.
Visit http://localhost:3000 to see an animated page.
Algernon comes with an auto-refresh feature that inserts a tiny bit of JavaScript into a page, watches files for changes and also serves file changed events as SSE (server sent events).
Here's one way to try it out (requires the -a flag):
- While still serving the page, and displaying it in the browser, change the numbers in
style.css, save the file and observe that the page in the browser instantly changes. - Also try changing numbers in
eye.svgandmouth.svg, save the file and watch the page instantly being updated.
What should the next step in this tutorial be? Please submit an issue with a suggestion. Thanks for reading! :)