Skip to content

Usage

Pages allow you to display and collect data using your Coronium Core server. You can display simple static web pages (.html), or serve dynamic pages using Lua.

Special Note

While the Pages module is capable of serving up complex websites, it is generally meant to supplement the core functionality of the application server.

Pages Directory

All pages and assets reside in the /home/coronium/pages directory, which is the "root" directory for all web based requests. Log in via SFTP to manage your page files.

Dynamic Pages

To create a dynamic web page, name the file with the .lua extension and use the new method to create a pages object instance. The browser path will be the path relative to the pages directory.

Call the Page file by addressing it by name, without the .lua extension.

Example Paths

File path: /home/coronium/pages/users/show.lua

Browser path: https://my.coronium.com/users/show

Requests

The Pages module supports both GET and POST requests. In most cases you should set up your page to handle both types of request. The following example demonstrates a common page request pattern:

-- mypage.lua
local page = core.pages.new()

-- GET handler
local function processGet()
  ...
end

-- POST handler
local function processPost()
  ...
end

--# Incoming Request
if page.isGet then
  -- GET request
  processGet()
elseif page.isPost then
  -- POST request
  processPost()
else
  -- Method not supported
  page.status(501)
end

Responses

Output responses to the client web browser by using the various "output" methods of the pages object instance. The available response types are HTML, JSON, or plain text.

HTML

To return content as html (text/html), pass a string body to the response method.

local page = core.pages.new()

local body = 
[[
  <html>
    <body>
      <h1>Hello Pages!</h1>
    </body>
  </html>
]]

page.response(body)

See also: Templates

JSON

To return content as JSON (application/json), use the renderJson method.

local page = core.pages.new()

--Uses a table for easy JSON construction
local data = {
  name = "Dani",
  age = 23
}

page.renderJson(data)

See also: page.response

TEXT

To return content as plain text (text/plain), use the renderText method.

local page = core.pages.new()

local text = "This is some plain text"

page.renderText(text)

See also: page.response

Queries

To access properties of a query string, use the query property of the pages object instance. The query is a table with name/value pairs.

Example Path

/mypage?name=Tim&age=42

Example Lua

--mypage.lua
local page = core.pages.new()

local function processPage()
  print(page.query.name) --Tim
  print(page.query.age) --42

  --Loop over query name/values
  for name, val in pairs(page.query) do
    print(name.."="..val)
  end
end

--# Incoming
if page.isGet then
  processPage()
else
  page.status(501)
end

Forms

To access properties of a posted form, use the form property of the pages object instance. The form is a table with name/value pairs.

Example Form

<form action="" method="POST">
  <input name="firstname" id="firstname">
  <input name="lastname" id="lastname">
  <button type="submit">Submit</button>
</form>

Example Lua

--myform.lua
local page = core.pages.new()

local function processForm()
  local first = page.form.firstname
  local last = page.form.lastname

  print(first.." "..last)

  --Loop over form name/values
  for name, val in pairs(page.form) do
    print(name.."="..val)
  end
end

--# Incoming
if page.isPost then
  processForm()
else
  page.status(501)
end

Uploads

To access file uploads, use the files property of the pages object instance.

Example Upload Form

<!-- upload.html -->
<form action="" method="POST" enctype="multipart/form-data">
  <input type="file" name="avatar" id="avatar">
  <button type="submit">Upload</button>
</form>

Example Lua

-- upload.lua
local page = core.pages.new()

local function processUpload()
  local avatarObj = page.files.avatar
  page.saveFile(avatarObj, '/avatars')
  page.response("Done")
end

--# Incoming
if page.isPost then
  processUpload()
else
  page.status(501)
end

See the File Uploads section.

Cookies

See Cookies in the Pages API documentation.

Headers

To access the request headers, use the headers property of the pages object instance. The headers are in a table as name/value pairs.

Example

local page = core.pages.new()

local headers = page.headers

--print "Host" header
print(headers["Host"])

--Loop over headers name/values
for name, val in pairs(headers) do
  print(name.."="..val)
end

Core Modules

You can use any of the core server modules in dynamic pages.

Example Template

<!-- user.tpl -->
<html>
  <body>
    <h1>{{ name }}</h1>
    <p>{{ age }}</p>
    <p>{{ _id }}</p>
  </body>
</html>

Example Lua

-- user.lua
local page = core.pages.new()

local function processPage()
  local users = core.data("users")
  local doc = users:get("id1234")

  page.render('/user.tpl', doc)
end

--# Incoming
if page.isGet then
  processPage()
else
  page.status(501)
end

Files Directory

Files uploaded to the files/public directory using the Files module can be displayed using page templates. When referencing a file in a template, the path will be checked in the pages directory first. If no file is found, then the files/public directory path will be checked.