🍮 PudimServer Docs

Practical examples

This page shows compact scenarios with a short explanation before each code snippet.

Minimal server

This scenario shows the basics: create a server, register a route, and return text.

local PudimServer = require("PudimServer")

local server = PudimServer:Create{ Port = 8080 }

server:Routes("/", function(req, res)
  return res:response(200, "Hello World!")
end)

server:Run()

CORS + pipeline + cache

This scenario adds CORS, HTTP middleware, and GET response caching.

local Cache = require("PudimServer.cache")

server:EnableCors()
server:UseHandler{ name = "logger", Handler = function(req, res, next)
  print(req.method, req.path)
  return next()
end }

local cache = Cache.new{ DefaultTTL = 30 }
server:UseHandler(Cache.createPipelineHandler(cache))

Query string and dynamic routes

Use req.query for URL parameters and req.params for dynamic segments.

server:Routes("/search", function(req, res)
  return res:response(200, { query = req.query })
end)

server:Routes("/users/:id", function(req, res)
  return res:response(200, { userId = req.params.id })
end)

HTTPS + cooperative concurrency

This scenario enables native TLS with luasec and coroutine-based cooperative processing.

local server = PudimServer:Create{
  Port = 8443,
  Https = {
    Enabled = true,
    Key = "./examples/certs/server.key",
    Certificate = "./examples/certs/server.crt"
  },
  Concurrency = { Enabled = true, Sleep = 0.001 }
}

Hot reload without file watcher

This scenario reloads modules in development by clearing package.loaded before each request.

local server = PudimServer:Create{
  Port = 8086,
  HotReload = {
    Enabled = true,
    Modules = {"examples.hot_reload_message"}
  }
}

server:Routes("/reload", function(req, res)
  local messageProvider = require("examples.hot_reload_message")
  return res:response(200, messageProvider.get())
end)