Server

class Opts

The options a server knows about

env: str

‘debug’|’production’ if debug, more information is provided on errors

backlog: number

The number to pass to socket:listen

static new(t)

Create a new options object

Parameters

t (table or nil) – If not the pre-set options

Return type

Opts

set_backlog(backlog)

Set the backlog property

Parameters

backlog (number) –

Return type

Opts

set_env(env)

Set the env property

Parameters

env (str) – ‘production’|’debug’ The env string

Return type

Opts

class Server

The primary interface for working with this framework it can be used to register middleware and route handlers

sock: table

socket being used by the server

router: Router

The router for incoming requests

middleware: table

List of middleware callbacks

ip: str

defaults to ‘0.0.0.0’

env: str

defaults to ‘production’

backlog: number or nil

defaults to nil

static new(opts)

Constructor for a Server that will use luasocket’s socket

implementation

Parameters

opts (Opts) – The configuration of this Server

static new_with(sock, opts)

Constructor for a Server that will use the provided socket

The socket provided must have a similar api to the luasocket’s tcp socket and also be compatible with cosock

Parameters
  • sock (table) – The socket to use

  • opts (Opts) – The configuration of this Server

set_ip(ip)

Override the default IP address

Parameters

ip (str) –

Return type

Server

listen(port)

Attempt to open a socket

Parameters

port (number or nil) – If provided, the port this server will attempt to bind on

Return type

Server

use(middleware)

Register some middleware to be use for each request

Parameters

middleware (fun(Request, Response, fun(Request, Response))) –

Return type

Server

route(req, res)

Route a request, first through any registered middleware

followed by any registered handler

Parameters
  • req (Request) –

  • res (Response) –

_tick(incoming)
Parameters

incoming (any) –

tick(err_callback)

A single step in the Server run loop

which will call accept on the underlying socket and when that returns a client socket, it will attempt to route the Request/Response objects through the registered middleware and routes

Parameters

err_callback (any) –

_run(err_callback, should_continue)
Parameters
  • err_callback (any) –

  • should_continue (any) –

run(err_callback)

Start this server, blocking forever

Parameters

err_callback (fun(str):boolean) – Optional callback to be run if tick returns an error

Registering Handlers

The Server table also provides a method for registering a handler for each of the HTTP methods. The name for each of these methods is formatted into lower snake case and they all take the path as the first argument and a function as the second argument and all return the server instance. The function argument will take 2 arguments, the Request first and the Response second. For example registering a GET and POST request handler for the / path you would use the following method calls.

server:get('/', function(req, res)
  res:send('')
end)
:post('/', function(req, res)
  res:send('')
end)

The following methods are available.

  • acl

  • bind

  • checkout

  • connect

  • copy

  • delete

  • get

  • head

  • link

  • lock

  • m_search

  • merge

  • mkactivity

  • mkcalendar

  • mkcol

  • move

  • notify

  • options

  • patch

  • post

  • propfind

  • proppatch

  • purge

  • put

  • rebind

  • report

  • search

  • subscribe

  • trace

  • unbind

  • unlink

  • unlock

  • unsubscribe