APItools / router.lua

A barebones router for Lua. It matches urls and executes lua functions.
MIT License
195 stars 47 forks source link

demo #30

Open JamesJoe-C opened 6 years ago

JamesJoe-C commented 6 years ago

require("route");

local router = require 'router' local r = router.new()

r:match(route)

local method = ngx.var.request_method; local post_data = "";

if(method == "POST") then ngx.req.read_body(); local post_data = ngx.req.get_post_args(); end

local ok, errmsg = r:execute( method, ngx.var.request_uri, ngx.req.get_uri_args(), -- all these parameters post_data, -- will be merged in order {other_arg = 1} ) -- into a single "params" table

if ok then ngx.status = 200 else ngx.exec("@client") -- ngx.status = 404 -- ngx.print("Not found!") -- ngx.log(ngx.ERROR, errmsg) end